Rev 257 | Rev 348 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
224 | palkovsky | 1 | # |
2 | # Copyright (C) 2005 Ondrej Palkovsky |
||
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 | |||
30 | # Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word |
||
31 | # and 1 means interrupt with error word |
||
32 | |||
33 | |||
34 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00 |
||
35 | |||
36 | #define __ASM__ |
||
37 | #include <arch/pm.h> |
||
38 | |||
39 | .text |
||
40 | .global interrupt_handlers |
||
41 | .global panic_printf |
||
42 | |||
43 | panic_printf: |
||
44 | movq $halt, (%rsp) |
||
45 | jmp printf |
||
46 | |||
252 | palkovsky | 47 | .global memcpy |
48 | memcpy: |
||
49 | jmp _memcpy |
||
50 | |||
51 | .global cpuid |
||
242 | palkovsky | 52 | .global has_cpuid |
53 | .global rdtsc |
||
251 | palkovsky | 54 | .global read_efer_flag |
55 | .global set_efer_flag |
||
56 | |||
252 | palkovsky | 57 | |
257 | palkovsky | 58 | # THIS IS USERSPACE CODE |
59 | .global utext |
||
60 | utext: |
||
61 | 0: |
||
62 | int $48 |
||
63 | jmp 0b |
||
64 | # not reached |
||
65 | utext_end: |
||
66 | |||
67 | .data |
||
68 | .global utext_size |
||
69 | utext_size: |
||
70 | .long utext_end - utext |
||
71 | |||
72 | |||
242 | palkovsky | 73 | ## Determine CPUID support |
74 | # |
||
75 | # Return 0 in EAX if CPUID is not support, 1 if supported. |
||
76 | # |
||
77 | has_cpuid: |
||
78 | pushq %rbx |
||
79 | |||
80 | pushfq # store flags |
||
81 | popq %rax # read flags |
||
82 | movq %rax,%rbx # copy flags |
||
83 | btcl $21,%ebx # swap the ID bit |
||
84 | pushq %rbx |
||
85 | popfq # propagate the change into flags |
||
86 | pushfq |
||
87 | popq %rbx # read flags |
||
88 | andl $(1<<21),%eax # interested only in ID bit |
||
89 | andl $(1<<21),%ebx |
||
90 | xorl %ebx,%eax # 0 if not supported, 1 if supported |
||
91 | |||
92 | popq %rbx |
||
93 | ret |
||
94 | |||
251 | palkovsky | 95 | cpuid: |
96 | movq %rbx, %r10 # we have to preserve rbx across function calls |
||
242 | palkovsky | 97 | |
251 | palkovsky | 98 | movl %edi,%eax # load the command into %eax |
99 | |||
100 | cpuid |
||
101 | movl %eax,0(%rsi) |
||
102 | movl %ebx,4(%rsi) |
||
103 | movl %ecx,8(%rsi) |
||
104 | movl %edx,12(%rsi) |
||
105 | |||
106 | movq %r10, %rbx |
||
107 | ret |
||
108 | |||
242 | palkovsky | 109 | rdtsc: |
110 | xorq %rax,%rax |
||
111 | rdtsc |
||
112 | ret |
||
251 | palkovsky | 113 | |
114 | set_efer_flag: |
||
115 | movq $0xc0000080, %rcx |
||
116 | rdmsr |
||
117 | btsl %edi, %eax |
||
118 | wrmsr |
||
119 | ret |
||
242 | palkovsky | 120 | |
251 | palkovsky | 121 | read_efer_flag: |
122 | movq $0xc0000080, %rcx |
||
123 | rdmsr |
||
124 | ret |
||
242 | palkovsky | 125 | |
224 | palkovsky | 126 | # Push all general purpose registers on stack except %rbp, %rsp |
127 | .macro push_all_gpr |
||
128 | pushq %rax |
||
129 | pushq %rbx |
||
130 | pushq %rcx |
||
131 | pushq %rdx |
||
132 | pushq %rsi |
||
133 | pushq %rdi |
||
134 | pushq %r8 |
||
135 | pushq %r9 |
||
136 | pushq %r10 |
||
137 | pushq %r11 |
||
138 | pushq %r12 |
||
139 | pushq %r13 |
||
140 | pushq %r14 |
||
141 | pushq %r15 |
||
142 | .endm |
||
143 | |||
144 | .macro pop_all_gpr |
||
145 | popq %r15 |
||
146 | popq %r14 |
||
147 | popq %r13 |
||
148 | popq %r12 |
||
149 | popq %r11 |
||
150 | popq %r10 |
||
151 | popq %r9 |
||
152 | popq %r8 |
||
153 | popq %rdi |
||
154 | popq %rsi |
||
155 | popq %rdx |
||
156 | popq %rcx |
||
157 | popq %rbx |
||
158 | popq %rax |
||
159 | .endm |
||
160 | |||
161 | ## Declare interrupt handlers |
||
162 | # |
||
163 | # Declare interrupt handlers for n interrupt |
||
164 | # vectors starting at vector i. |
||
165 | # |
||
166 | # The handlers setup data segment registers |
||
167 | # and call trap_dispatcher(). |
||
168 | # |
||
169 | .macro handler i n |
||
170 | pushq %rbp |
||
171 | movq %rsp,%rbp |
||
172 | |||
173 | push_all_gpr |
||
174 | |||
175 | movq $(\i),%rdi # %rdi - first parameter |
||
176 | movq %rbp, %rsi |
||
177 | addq $8, %rsi # %rsi - second parameter - original stack |
||
332 | palkovsky | 178 | call trap_dispatcher # trap_dispatcher(i, stack) |
224 | palkovsky | 179 | |
180 | # Test if this is interrupt with error word or not |
||
181 | mov $\i,%cl; |
||
182 | movl $1,%eax; |
||
183 | test $0xe0,%cl; |
||
184 | jnz 0f; |
||
185 | and $0x1f,%cl; |
||
186 | shl %cl,%eax; |
||
187 | and $ERROR_WORD_INTERRUPT_LIST,%eax; |
||
188 | jz 0f; |
||
189 | |||
190 | |||
191 | # Return with error word |
||
192 | pop_all_gpr |
||
193 | |||
194 | popq %rbp; |
||
195 | add $8,%esp; # Skip error word |
||
196 | iretq |
||
197 | |||
198 | 0: |
||
199 | # Return with no error word |
||
200 | pop_all_gpr |
||
201 | |||
202 | popq %rbp |
||
203 | iretq |
||
204 | |||
205 | .if (\n-\i)-1 |
||
206 | handler "(\i+1)",\n |
||
207 | .endif |
||
208 | .endm |
||
209 | |||
210 | interrupt_handlers: |
||
211 | h_start: |
||
212 | handler 0 IDT_ITEMS |
||
213 | h_end: |
||
214 | |||
215 | |||
216 | .data |
||
217 | .global interrupt_handler_size |
||
218 | |||
219 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS |