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