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