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