| 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 |
|
|---|
| 29 | ## very low and hardware-level functions
|
|---|
| 30 |
|
|---|
| 31 | .text
|
|---|
| 32 |
|
|---|
| 33 | .global cpu_halt
|
|---|
| 34 | .global cpu_sleep
|
|---|
| 35 | .global paging_on
|
|---|
| 36 | .global enable_l_apic_in_msr
|
|---|
| 37 | .global interrupt_handlers
|
|---|
| 38 | .global inb
|
|---|
| 39 | .global inw
|
|---|
| 40 | .global inl
|
|---|
| 41 | .global outb
|
|---|
| 42 | .global outw
|
|---|
| 43 | .global outl
|
|---|
| 44 | .global memcopy
|
|---|
| 45 | .global memsetb
|
|---|
| 46 | .global memsetw
|
|---|
| 47 | .global memcmp
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | ## Turn paging on
|
|---|
| 51 | #
|
|---|
| 52 | # Enable paging and write-back caching in CR0.
|
|---|
| 53 | #
|
|---|
| 54 | paging_on:
|
|---|
| 55 | pushl %eax
|
|---|
| 56 | movl %cr0,%eax
|
|---|
| 57 | orl $(1<<31),%eax # paging on
|
|---|
| 58 | andl $~((1<<30)|(1<<29)),%eax # clear Cache Disable and not Write Though
|
|---|
| 59 | movl %eax,%cr0
|
|---|
| 60 | jmp 0f
|
|---|
| 61 | 0:
|
|---|
| 62 | popl %eax
|
|---|
| 63 | ret
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | ## Enable local APIC
|
|---|
| 67 | #
|
|---|
| 68 | # Enable local APIC in MSR.
|
|---|
| 69 | #
|
|---|
| 70 | enable_l_apic_in_msr:
|
|---|
| 71 | pusha
|
|---|
| 72 |
|
|---|
| 73 | movl $0x1b, %ecx
|
|---|
| 74 | rdmsr
|
|---|
| 75 | orl $(1<<11),%eax
|
|---|
| 76 | orl $(0xfee00000),%eax
|
|---|
| 77 | wrmsr
|
|---|
| 78 |
|
|---|
| 79 | popa
|
|---|
| 80 | ret
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | ## Declare interrupt handlers
|
|---|
| 84 | #
|
|---|
| 85 | # Declare interrupt handlers for n interrupt
|
|---|
| 86 | # vectors starting at vector i.
|
|---|
| 87 | #
|
|---|
| 88 | # The handlers setup data segment registers
|
|---|
| 89 | # and call trap_dispatcher().
|
|---|
| 90 | #
|
|---|
| 91 | .macro handler i n
|
|---|
| 92 | push %ebp
|
|---|
| 93 | movl %esp,%ebp
|
|---|
| 94 | pusha
|
|---|
| 95 |
|
|---|
| 96 | push %ds
|
|---|
| 97 | push %es
|
|---|
| 98 |
|
|---|
| 99 | # we must fill the data segment registers
|
|---|
| 100 | movw $16,%ax
|
|---|
| 101 | movw %ax,%ds
|
|---|
| 102 | movw %ax,%es
|
|---|
| 103 |
|
|---|
| 104 | movl $(\i),%edi
|
|---|
| 105 | pushl %ebp
|
|---|
| 106 | addl $4,(%esp)
|
|---|
| 107 | pushl %edi
|
|---|
| 108 | call trap_dispatcher
|
|---|
| 109 | addl $8,%esp
|
|---|
| 110 |
|
|---|
| 111 | pop %es
|
|---|
| 112 | pop %ds
|
|---|
| 113 |
|
|---|
| 114 | popa
|
|---|
| 115 | pop %ebp
|
|---|
| 116 |
|
|---|
| 117 | iret
|
|---|
| 118 |
|
|---|
| 119 | .if (\n-\i)-1
|
|---|
| 120 | handler "(\i+1)",\n
|
|---|
| 121 | .endif
|
|---|
| 122 | .endm
|
|---|
| 123 |
|
|---|
| 124 | # keep in sync with pm.h !!!
|
|---|
| 125 | IDT_ITEMS=64
|
|---|
| 126 | interrupt_handlers:
|
|---|
| 127 | h_start:
|
|---|
| 128 | handler 0 64
|
|---|
| 129 | # handler 64 128
|
|---|
| 130 | # handler 128 192
|
|---|
| 131 | # handler 192 256
|
|---|
| 132 | h_end:
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | ## I/O input (byte)
|
|---|
| 136 | #
|
|---|
| 137 | # Get a byte from I/O port and store it AL.
|
|---|
| 138 | #
|
|---|
| 139 | inb:
|
|---|
| 140 | push %edx
|
|---|
| 141 | xorl %eax,%eax
|
|---|
| 142 | movl 8(%esp),%edx
|
|---|
| 143 | inb %dx,%al
|
|---|
| 144 | pop %edx
|
|---|
| 145 | ret
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | ## I/O input (word)
|
|---|
| 149 | #
|
|---|
| 150 | # Get a word from I/O port and store it AX.
|
|---|
| 151 | #
|
|---|
| 152 | inw:
|
|---|
| 153 | push %edx
|
|---|
| 154 | xorl %eax,%eax
|
|---|
| 155 | movl 8(%esp),%edx
|
|---|
| 156 | inw %dx,%ax
|
|---|
| 157 | pop %edx
|
|---|
| 158 | ret
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | ## I/O input (dword)
|
|---|
| 162 | #
|
|---|
| 163 | # Get a dword from I/O port and store it EAX.
|
|---|
| 164 | #
|
|---|
| 165 | inl:
|
|---|
| 166 | push %edx
|
|---|
| 167 | xorl %eax,%eax
|
|---|
| 168 | movl 8(%esp),%edx
|
|---|
| 169 | inl %dx,%eax
|
|---|
| 170 | pop %edx
|
|---|
| 171 | ret
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | ## I/O output (byte)
|
|---|
| 175 | #
|
|---|
| 176 | # Send a byte to I/O port.
|
|---|
| 177 | #
|
|---|
| 178 | outb:
|
|---|
| 179 | push %ebp
|
|---|
| 180 | movl %esp,%ebp
|
|---|
| 181 | pusha
|
|---|
| 182 |
|
|---|
| 183 | movl 8(%ebp),%edx
|
|---|
| 184 | movl 12(%ebp),%eax
|
|---|
| 185 | outb %al,%dx
|
|---|
| 186 |
|
|---|
| 187 | popa
|
|---|
| 188 | pop %ebp
|
|---|
| 189 | ret
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | ## I/O output (word)
|
|---|
| 193 | #
|
|---|
| 194 | # Send a word to I/O port.
|
|---|
| 195 | #
|
|---|
| 196 | outw:
|
|---|
| 197 | push %ebp
|
|---|
| 198 | movl %esp,%ebp
|
|---|
| 199 | pusha
|
|---|
| 200 |
|
|---|
| 201 | movl 8(%ebp),%edx
|
|---|
| 202 | movl 12(%ebp),%eax
|
|---|
| 203 | outw %ax,%dx
|
|---|
| 204 |
|
|---|
| 205 | popa
|
|---|
| 206 | pop %ebp
|
|---|
| 207 | ret
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 | ## I/O output (dword)
|
|---|
| 211 | #
|
|---|
| 212 | # Send a dword to I/O port.
|
|---|
| 213 | #
|
|---|
| 214 | outl:
|
|---|
| 215 | push %ebp
|
|---|
| 216 | movl %esp,%ebp
|
|---|
| 217 | pusha
|
|---|
| 218 |
|
|---|
| 219 | movl 8(%ebp),%edx
|
|---|
| 220 | movl 12(%ebp),%eax
|
|---|
| 221 | outl %eax,%dx
|
|---|
| 222 |
|
|---|
| 223 | popa
|
|---|
| 224 | pop %ebp
|
|---|
| 225 | ret
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 | ## Copy memory
|
|---|
| 229 | #
|
|---|
| 230 | # Copy a given number of bytes (3rd argument)
|
|---|
| 231 | # from the memory location defined by 1st argument
|
|---|
| 232 | # to the memory location defined by 2nd argument.
|
|---|
| 233 | # The memory areas cannot overlap.
|
|---|
| 234 | #
|
|---|
| 235 | SRC=8
|
|---|
| 236 | DST=12
|
|---|
| 237 | CNT=16
|
|---|
| 238 | memcopy:
|
|---|
| 239 | push %ebp
|
|---|
| 240 | movl %esp,%ebp
|
|---|
| 241 | pusha
|
|---|
| 242 |
|
|---|
| 243 | cld
|
|---|
| 244 | movl CNT(%ebp),%ecx
|
|---|
| 245 | movl DST(%ebp),%edi
|
|---|
| 246 | movl SRC(%ebp),%esi
|
|---|
| 247 |
|
|---|
| 248 | rep movsb %ds:(%esi),%es:(%edi)
|
|---|
| 249 |
|
|---|
| 250 | popa
|
|---|
| 251 | pop %ebp
|
|---|
| 252 | ret
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 | ## Fill memory with bytes
|
|---|
| 256 | #
|
|---|
| 257 | # Fill a given number of bytes (2nd argument)
|
|---|
| 258 | # at memory defined by 1st argument with the
|
|---|
| 259 | # byte value defined by 3rd argument.
|
|---|
| 260 | #
|
|---|
| 261 | DST=8
|
|---|
| 262 | CNT=12
|
|---|
| 263 | X=16
|
|---|
| 264 | memsetb:
|
|---|
| 265 | push %ebp
|
|---|
| 266 | movl %esp,%ebp
|
|---|
| 267 | pusha
|
|---|
| 268 |
|
|---|
| 269 | cld
|
|---|
| 270 | movl CNT(%ebp),%ecx
|
|---|
| 271 | movl DST(%ebp),%edi
|
|---|
| 272 | movl X(%ebp),%eax
|
|---|
| 273 |
|
|---|
| 274 | rep stosb %al,%es:(%edi)
|
|---|
| 275 |
|
|---|
| 276 | popa
|
|---|
| 277 | pop %ebp
|
|---|
| 278 | ret
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 | ## Fill memory with words
|
|---|
| 282 | #
|
|---|
| 283 | # Fill a given number of words (2nd argument)
|
|---|
| 284 | # at memory defined by 1st argument with the
|
|---|
| 285 | # word value defined by 3rd argument.
|
|---|
| 286 | #
|
|---|
| 287 | DST=8
|
|---|
| 288 | CNT=12
|
|---|
| 289 | X=16
|
|---|
| 290 | memsetw:
|
|---|
| 291 | push %ebp
|
|---|
| 292 | movl %esp,%ebp
|
|---|
| 293 | pusha
|
|---|
| 294 |
|
|---|
| 295 | cld
|
|---|
| 296 | movl CNT(%ebp),%ecx
|
|---|
| 297 | movl DST(%ebp),%edi
|
|---|
| 298 | movl X(%ebp),%eax
|
|---|
| 299 |
|
|---|
| 300 | rep stosw %ax,%es:(%edi)
|
|---|
| 301 |
|
|---|
| 302 | popa
|
|---|
| 303 | pop %ebp
|
|---|
| 304 | ret
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 | ## Compare memory regions for equality
|
|---|
| 308 | #
|
|---|
| 309 | # Compare a given number of bytes (3rd argument)
|
|---|
| 310 | # at memory locations defined by 1st and 2nd argument
|
|---|
| 311 | # for equality. If the bytes are equal, EAX contains
|
|---|
| 312 | # 0.
|
|---|
| 313 | #
|
|---|
| 314 | SRC=12
|
|---|
| 315 | DST=16
|
|---|
| 316 | CNT=20
|
|---|
| 317 | memcmp:
|
|---|
| 318 | push %ebp
|
|---|
| 319 | subl $4,%esp
|
|---|
| 320 | movl %esp,%ebp
|
|---|
| 321 |
|
|---|
| 322 | pusha
|
|---|
| 323 |
|
|---|
| 324 | cld
|
|---|
| 325 | movl CNT(%ebp),%ecx
|
|---|
| 326 | movl DST(%ebp),%edi
|
|---|
| 327 | movl SRC(%ebp),%esi
|
|---|
| 328 |
|
|---|
| 329 | repe cmpsb %es:(%edi),%ds:(%esi)
|
|---|
| 330 | movl %ecx,(%ebp)
|
|---|
| 331 |
|
|---|
| 332 | popa
|
|---|
| 333 |
|
|---|
| 334 | movl (%ebp),%eax # return value => %eax (zero on success)
|
|---|
| 335 | addl $4,%esp
|
|---|
| 336 | pop %ebp
|
|---|
| 337 |
|
|---|
| 338 | ret
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 | # THIS IS USERSPACE CODE
|
|---|
| 342 | .global utext
|
|---|
| 343 | utext:
|
|---|
| 344 | 0:
|
|---|
| 345 | # movl $0xdeadbeaf, %eax
|
|---|
| 346 | int $48
|
|---|
| 347 | jmp 0b
|
|---|
| 348 | # not reached
|
|---|
| 349 | utext_end:
|
|---|
| 350 |
|
|---|
| 351 | .data
|
|---|
| 352 | .global utext_size
|
|---|
| 353 | utext_size:
|
|---|
| 354 | .long utext_end - utext
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 | #.section K_DATA_START
|
|---|
| 358 | .global interrupt_handler_size
|
|---|
| 359 |
|
|---|
| 360 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
|
|---|