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