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