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