[f761f1eb] | 1 | #
|
---|
| 2 | # Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 3 | # All rights reserved.
|
---|
| 4 | #
|
---|
| 5 | # Redistribution and use in source and binary forms, with or without
|
---|
| 6 | # modification, are permitted provided that the following conditions
|
---|
| 7 | # are met:
|
---|
| 8 | #
|
---|
| 9 | # - Redistributions of source code must retain the above copyright
|
---|
| 10 | # notice, this list of conditions and the following disclaimer.
|
---|
| 11 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | # documentation and/or other materials provided with the distribution.
|
---|
| 14 | # - The name of the author may not be used to endorse or promote products
|
---|
| 15 | # derived from this software without specific prior written permission.
|
---|
| 16 | #
|
---|
| 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | #
|
---|
| 28 |
|
---|
[da585a52] | 29 | ## very low and hardware-level functions
|
---|
[f761f1eb] | 30 |
|
---|
| 31 | .text
|
---|
| 32 |
|
---|
| 33 | .global cpu_priority_high
|
---|
| 34 | .global cpu_priority_low
|
---|
| 35 | .global cpu_priority_restore
|
---|
| 36 | .global cpu_priority_read
|
---|
| 37 | .global cpu_halt
|
---|
| 38 | .global cpu_sleep
|
---|
| 39 | .global paging_on
|
---|
| 40 | .global cpu_read_dba
|
---|
| 41 | .global cpu_write_dba
|
---|
| 42 | .global cpu_read_cr2
|
---|
| 43 | .global enable_l_apic_in_msr
|
---|
| 44 | .global interrupt_handlers
|
---|
| 45 | .global inb
|
---|
| 46 | .global inw
|
---|
| 47 | .global inl
|
---|
| 48 | .global outb
|
---|
| 49 | .global outw
|
---|
| 50 | .global outl
|
---|
| 51 | .global memcopy
|
---|
| 52 | .global memsetb
|
---|
| 53 | .global memsetw
|
---|
| 54 | .global memcmp
|
---|
| 55 |
|
---|
[da585a52] | 56 |
|
---|
| 57 | ## Set priority level high
|
---|
| 58 | #
|
---|
| 59 | # Disable interrupts and return previous
|
---|
| 60 | # EFLAGS in EAX.
|
---|
[f761f1eb] | 61 | #
|
---|
| 62 | cpu_priority_high:
|
---|
| 63 | pushf
|
---|
| 64 | pop %eax
|
---|
| 65 | cli
|
---|
| 66 | ret
|
---|
[da585a52] | 67 |
|
---|
| 68 |
|
---|
| 69 | ## Set priority level low
|
---|
[f761f1eb] | 70 | #
|
---|
[da585a52] | 71 | # Enable interrupts and return previous
|
---|
| 72 | # EFLAGS in EAX.
|
---|
| 73 | #
|
---|
[f761f1eb] | 74 | cpu_priority_low:
|
---|
| 75 | pushf
|
---|
| 76 | pop %eax
|
---|
| 77 | sti
|
---|
| 78 | ret
|
---|
| 79 |
|
---|
[da585a52] | 80 |
|
---|
| 81 | ## Restore priority level
|
---|
| 82 | #
|
---|
| 83 | # Restore EFLAGS.
|
---|
[f761f1eb] | 84 | #
|
---|
| 85 | cpu_priority_restore:
|
---|
| 86 | push 4(%esp)
|
---|
| 87 | popf
|
---|
| 88 | ret
|
---|
| 89 |
|
---|
[da585a52] | 90 | ## Return raw priority level
|
---|
| 91 | #
|
---|
| 92 | # Return EFLAFS in EAX.
|
---|
| 93 | #
|
---|
[f761f1eb] | 94 | cpu_priority_read:
|
---|
| 95 | pushf
|
---|
| 96 | pop %eax
|
---|
| 97 | ret
|
---|
| 98 |
|
---|
[da585a52] | 99 |
|
---|
| 100 | ## Halt the CPU
|
---|
| 101 | #
|
---|
| 102 | # Halt the CPU using HLT.
|
---|
| 103 | #
|
---|
[f761f1eb] | 104 | cpu_halt:
|
---|
| 105 | cpu_sleep:
|
---|
| 106 | hlt
|
---|
| 107 | ret
|
---|
| 108 |
|
---|
[da585a52] | 109 |
|
---|
| 110 | ## Turn paging on
|
---|
| 111 | #
|
---|
| 112 | # Enable paging and write-back caching in CR0.
|
---|
| 113 | #
|
---|
[f761f1eb] | 114 | paging_on:
|
---|
| 115 | pushl %eax
|
---|
| 116 | movl %cr0,%eax
|
---|
| 117 | orl $(1<<31),%eax # paging on
|
---|
| 118 | andl $~((1<<30)|(1<<29)),%eax # clear Cache Disable and not Write Though
|
---|
| 119 | movl %eax,%cr0
|
---|
| 120 | jmp 0f
|
---|
| 121 | 0:
|
---|
| 122 | popl %eax
|
---|
| 123 | ret
|
---|
| 124 |
|
---|
[da585a52] | 125 |
|
---|
| 126 | ## Read CR3
|
---|
| 127 | #
|
---|
| 128 | # Store CR3 in EAX.
|
---|
| 129 | #
|
---|
[f761f1eb] | 130 | cpu_read_dba:
|
---|
| 131 | movl %cr3,%eax
|
---|
| 132 | ret
|
---|
| 133 |
|
---|
[da585a52] | 134 |
|
---|
| 135 | ## Write CR3
|
---|
| 136 | #
|
---|
| 137 | # Set CR3.
|
---|
| 138 | #
|
---|
[f761f1eb] | 139 | cpu_write_dba:
|
---|
| 140 | pushl %eax
|
---|
| 141 | movl 8(%esp),%eax
|
---|
| 142 | movl %eax,%cr3
|
---|
| 143 | popl %eax
|
---|
| 144 | ret
|
---|
| 145 |
|
---|
[da585a52] | 146 |
|
---|
| 147 | ## Read CR2
|
---|
| 148 | #
|
---|
| 149 | # Store CR2 in EAX.
|
---|
| 150 | #
|
---|
[f761f1eb] | 151 | cpu_read_cr2:
|
---|
| 152 | movl %cr2,%eax
|
---|
| 153 | ret
|
---|
| 154 |
|
---|
[da585a52] | 155 |
|
---|
| 156 | ## Enable local APIC
|
---|
| 157 | #
|
---|
| 158 | # Enable local APIC in MSR.
|
---|
| 159 | #
|
---|
[f761f1eb] | 160 | enable_l_apic_in_msr:
|
---|
| 161 | pusha
|
---|
| 162 |
|
---|
| 163 | movl $0x1b, %ecx
|
---|
| 164 | rdmsr
|
---|
| 165 | orl $(1<<11),%eax
|
---|
| 166 | orl $(0xfee00000),%eax
|
---|
| 167 | wrmsr
|
---|
| 168 |
|
---|
| 169 | popa
|
---|
| 170 | ret
|
---|
| 171 |
|
---|
[da585a52] | 172 |
|
---|
| 173 | ## Declare interrupt handlers
|
---|
| 174 | #
|
---|
| 175 | # Declare interrupt handlers for n interrupt
|
---|
| 176 | # vectors starting at vector i.
|
---|
| 177 | #
|
---|
| 178 | # The handlers setup data segment registers
|
---|
| 179 | # and call trap_dispatcher().
|
---|
| 180 | #
|
---|
[f761f1eb] | 181 | .macro handler i n
|
---|
| 182 | push %ebp
|
---|
| 183 | movl %esp,%ebp
|
---|
| 184 | pusha
|
---|
[f07bba5] | 185 |
|
---|
| 186 | push %ds
|
---|
| 187 | push %es
|
---|
[f761f1eb] | 188 |
|
---|
| 189 | # we must fill the data segment registers
|
---|
| 190 | movw $16,%ax
|
---|
| 191 | movw %ax,%ds
|
---|
| 192 | movw %ax,%es
|
---|
| 193 |
|
---|
| 194 | movl $(\i),%edi
|
---|
| 195 | pushl %ebp
|
---|
| 196 | addl $4,(%esp)
|
---|
| 197 | pushl %edi
|
---|
| 198 | call trap_dispatcher
|
---|
| 199 | addl $8,%esp
|
---|
| 200 |
|
---|
[f07bba5] | 201 | pop %es
|
---|
| 202 | pop %ds
|
---|
| 203 |
|
---|
[f761f1eb] | 204 | popa
|
---|
| 205 | pop %ebp
|
---|
| 206 |
|
---|
| 207 | iret
|
---|
| 208 |
|
---|
| 209 | .if (\n-\i)-1
|
---|
| 210 | handler "(\i+1)",\n
|
---|
| 211 | .endif
|
---|
| 212 | .endm
|
---|
| 213 |
|
---|
| 214 | # keep in sync with pm.h !!!
|
---|
| 215 | IDT_ITEMS=64
|
---|
| 216 | interrupt_handlers:
|
---|
| 217 | h_start:
|
---|
| 218 | handler 0 64
|
---|
| 219 | # handler 64 128
|
---|
| 220 | # handler 128 192
|
---|
| 221 | # handler 192 256
|
---|
| 222 | h_end:
|
---|
| 223 |
|
---|
| 224 |
|
---|
[da585a52] | 225 | ## I/O input (byte)
|
---|
| 226 | #
|
---|
| 227 | # Get a byte from I/O port and store it AL.
|
---|
| 228 | #
|
---|
[f761f1eb] | 229 | inb:
|
---|
| 230 | push %edx
|
---|
| 231 | xorl %eax,%eax
|
---|
| 232 | movl 8(%esp),%edx
|
---|
| 233 | inb %dx,%al
|
---|
| 234 | pop %edx
|
---|
| 235 | ret
|
---|
| 236 |
|
---|
[da585a52] | 237 |
|
---|
| 238 | ## I/O input (word)
|
---|
| 239 | #
|
---|
| 240 | # Get a word from I/O port and store it AX.
|
---|
| 241 | #
|
---|
[f761f1eb] | 242 | inw:
|
---|
| 243 | push %edx
|
---|
| 244 | xorl %eax,%eax
|
---|
| 245 | movl 8(%esp),%edx
|
---|
| 246 | inw %dx,%ax
|
---|
| 247 | pop %edx
|
---|
| 248 | ret
|
---|
| 249 |
|
---|
[da585a52] | 250 |
|
---|
| 251 | ## I/O input (dword)
|
---|
| 252 | #
|
---|
| 253 | # Get a dword from I/O port and store it EAX.
|
---|
| 254 | #
|
---|
[f761f1eb] | 255 | inl:
|
---|
| 256 | push %edx
|
---|
| 257 | xorl %eax,%eax
|
---|
| 258 | movl 8(%esp),%edx
|
---|
| 259 | inl %dx,%eax
|
---|
| 260 | pop %edx
|
---|
| 261 | ret
|
---|
| 262 |
|
---|
[da585a52] | 263 |
|
---|
| 264 | ## I/O output (byte)
|
---|
| 265 | #
|
---|
| 266 | # Send a byte to I/O port.
|
---|
| 267 | #
|
---|
[f761f1eb] | 268 | outb:
|
---|
| 269 | push %ebp
|
---|
| 270 | movl %esp,%ebp
|
---|
| 271 | pusha
|
---|
| 272 |
|
---|
| 273 | movl 8(%ebp),%edx
|
---|
| 274 | movl 12(%ebp),%eax
|
---|
| 275 | outb %al,%dx
|
---|
| 276 |
|
---|
| 277 | popa
|
---|
| 278 | pop %ebp
|
---|
| 279 | ret
|
---|
| 280 |
|
---|
[da585a52] | 281 |
|
---|
| 282 | ## I/O output (word)
|
---|
| 283 | #
|
---|
| 284 | # Send a word to I/O port.
|
---|
| 285 | #
|
---|
[f761f1eb] | 286 | outw:
|
---|
| 287 | push %ebp
|
---|
| 288 | movl %esp,%ebp
|
---|
| 289 | pusha
|
---|
| 290 |
|
---|
| 291 | movl 8(%ebp),%edx
|
---|
| 292 | movl 12(%ebp),%eax
|
---|
| 293 | outw %ax,%dx
|
---|
| 294 |
|
---|
| 295 | popa
|
---|
| 296 | pop %ebp
|
---|
| 297 | ret
|
---|
| 298 |
|
---|
[da585a52] | 299 |
|
---|
| 300 | ## I/O output (dword)
|
---|
| 301 | #
|
---|
| 302 | # Send a dword to I/O port.
|
---|
| 303 | #
|
---|
[f761f1eb] | 304 | outl:
|
---|
| 305 | push %ebp
|
---|
| 306 | movl %esp,%ebp
|
---|
| 307 | pusha
|
---|
| 308 |
|
---|
| 309 | movl 8(%ebp),%edx
|
---|
| 310 | movl 12(%ebp),%eax
|
---|
| 311 | outl %eax,%dx
|
---|
| 312 |
|
---|
| 313 | popa
|
---|
| 314 | pop %ebp
|
---|
| 315 | ret
|
---|
| 316 |
|
---|
[da585a52] | 317 |
|
---|
| 318 | ## Copy memory
|
---|
| 319 | #
|
---|
| 320 | # Copy a given number of bytes (3rd argument)
|
---|
| 321 | # from the memory location defined by 1st argument
|
---|
| 322 | # to the memory location defined by 2nd argument.
|
---|
| 323 | # The memory areas cannot overlap.
|
---|
| 324 | #
|
---|
[f761f1eb] | 325 | SRC=8
|
---|
| 326 | DST=12
|
---|
| 327 | CNT=16
|
---|
| 328 | memcopy:
|
---|
| 329 | push %ebp
|
---|
| 330 | movl %esp,%ebp
|
---|
| 331 | pusha
|
---|
| 332 |
|
---|
| 333 | cld
|
---|
| 334 | movl CNT(%ebp),%ecx
|
---|
| 335 | movl DST(%ebp),%edi
|
---|
| 336 | movl SRC(%ebp),%esi
|
---|
| 337 |
|
---|
| 338 | rep movsb %ds:(%esi),%es:(%edi)
|
---|
| 339 |
|
---|
| 340 | popa
|
---|
| 341 | pop %ebp
|
---|
| 342 | ret
|
---|
| 343 |
|
---|
[da585a52] | 344 |
|
---|
| 345 | ## Fill memory with bytes
|
---|
| 346 | #
|
---|
| 347 | # Fill a given number of bytes (2nd argument)
|
---|
| 348 | # at memory defined by 1st argument with the
|
---|
| 349 | # byte value defined by 3rd argument.
|
---|
| 350 | #
|
---|
[f761f1eb] | 351 | DST=8
|
---|
| 352 | CNT=12
|
---|
| 353 | X=16
|
---|
[da585a52] | 354 | memsetb:
|
---|
[f761f1eb] | 355 | push %ebp
|
---|
| 356 | movl %esp,%ebp
|
---|
| 357 | pusha
|
---|
| 358 |
|
---|
| 359 | cld
|
---|
| 360 | movl CNT(%ebp),%ecx
|
---|
| 361 | movl DST(%ebp),%edi
|
---|
| 362 | movl X(%ebp),%eax
|
---|
| 363 |
|
---|
[da585a52] | 364 | rep stosb %al,%es:(%edi)
|
---|
[f761f1eb] | 365 |
|
---|
| 366 | popa
|
---|
| 367 | pop %ebp
|
---|
| 368 | ret
|
---|
| 369 |
|
---|
[da585a52] | 370 |
|
---|
| 371 | ## Fill memory with words
|
---|
| 372 | #
|
---|
| 373 | # Fill a given number of words (2nd argument)
|
---|
| 374 | # at memory defined by 1st argument with the
|
---|
| 375 | # word value defined by 3rd argument.
|
---|
| 376 | #
|
---|
[f761f1eb] | 377 | DST=8
|
---|
| 378 | CNT=12
|
---|
| 379 | X=16
|
---|
[da585a52] | 380 | memsetw:
|
---|
[f761f1eb] | 381 | push %ebp
|
---|
| 382 | movl %esp,%ebp
|
---|
| 383 | pusha
|
---|
| 384 |
|
---|
| 385 | cld
|
---|
| 386 | movl CNT(%ebp),%ecx
|
---|
| 387 | movl DST(%ebp),%edi
|
---|
| 388 | movl X(%ebp),%eax
|
---|
| 389 |
|
---|
[da585a52] | 390 | rep stosw %ax,%es:(%edi)
|
---|
[f761f1eb] | 391 |
|
---|
| 392 | popa
|
---|
| 393 | pop %ebp
|
---|
| 394 | ret
|
---|
| 395 |
|
---|
[da585a52] | 396 |
|
---|
| 397 | ## Compare memory regions for equality
|
---|
| 398 | #
|
---|
| 399 | # Compare a given number of bytes (3rd argument)
|
---|
| 400 | # at memory locations defined by 1st and 2nd argument
|
---|
| 401 | # for equality. If the bytes are equal, EAX contains
|
---|
| 402 | # 0.
|
---|
| 403 | #
|
---|
[f761f1eb] | 404 | SRC=12
|
---|
| 405 | DST=16
|
---|
| 406 | CNT=20
|
---|
| 407 | memcmp:
|
---|
| 408 | push %ebp
|
---|
| 409 | subl $4,%esp
|
---|
| 410 | movl %esp,%ebp
|
---|
| 411 |
|
---|
| 412 | pusha
|
---|
| 413 |
|
---|
| 414 | cld
|
---|
| 415 | movl CNT(%ebp),%ecx
|
---|
| 416 | movl DST(%ebp),%edi
|
---|
| 417 | movl SRC(%ebp),%esi
|
---|
| 418 |
|
---|
| 419 | repe cmpsb %es:(%edi),%ds:(%esi)
|
---|
| 420 | movl %ecx,(%ebp)
|
---|
| 421 |
|
---|
| 422 | popa
|
---|
| 423 |
|
---|
| 424 | movl (%ebp),%eax # return value => %eax (zero on success)
|
---|
| 425 | addl $4,%esp
|
---|
| 426 | pop %ebp
|
---|
| 427 |
|
---|
| 428 | ret
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | # THIS IS USERSPACE CODE
|
---|
| 432 | .global utext
|
---|
| 433 | utext:
|
---|
| 434 | 0:
|
---|
[f07bba5] | 435 | # movl $0xdeadbeaf, %eax
|
---|
[f761f1eb] | 436 | int $48
|
---|
| 437 | jmp 0b
|
---|
| 438 | # not reached
|
---|
| 439 | utext_end:
|
---|
| 440 |
|
---|
| 441 | .data
|
---|
| 442 | .global utext_size
|
---|
| 443 | utext_size:
|
---|
| 444 | .long utext_end - utext
|
---|
| 445 |
|
---|
| 446 |
|
---|
[ac5d02b] | 447 | #.section K_DATA_START
|
---|
[f761f1eb] | 448 | .global interrupt_handler_size
|
---|
| 449 |
|
---|
| 450 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
|
---|