Rev 1888 | Rev 2082 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
393 | bondari | 3 | * Copyright (C) 2005 Sergey Bondari |
1 | jermar | 4 | * All rights reserved. |
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
1888 | jermar | 30 | /** @addtogroup ia32 |
1702 | cejka | 31 | * @{ |
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
1888 | jermar | 36 | #ifndef KERN_ia32_ASM_H_ |
37 | #define KERN_ia32_ASM_H_ |
||
1 | jermar | 38 | |
1186 | jermar | 39 | #include <arch/pm.h> |
1 | jermar | 40 | #include <arch/types.h> |
177 | jermar | 41 | #include <config.h> |
1 | jermar | 42 | |
1780 | jermar | 43 | extern uint32_t interrupt_handler_size; |
1 | jermar | 44 | |
45 | extern void paging_on(void); |
||
46 | |||
47 | extern void interrupt_handlers(void); |
||
48 | |||
49 | extern void enable_l_apic_in_msr(void); |
||
50 | |||
195 | vana | 51 | |
1780 | jermar | 52 | extern void asm_delay_loop(uint32_t t); |
53 | extern void asm_fake_loop(uint32_t t); |
||
195 | vana | 54 | |
55 | |||
115 | jermar | 56 | /** Halt CPU |
57 | * |
||
58 | * Halt the current CPU until interrupt event. |
||
59 | */ |
||
348 | jermar | 60 | static inline void cpu_halt(void) { __asm__("hlt\n"); }; |
61 | static inline void cpu_sleep(void) { __asm__("hlt\n"); }; |
||
1 | jermar | 62 | |
1780 | jermar | 63 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \ |
1074 | palkovsky | 64 | { \ |
1780 | jermar | 65 | unative_t res; \ |
1074 | palkovsky | 66 | __asm__ volatile ("movl %%" #reg ", %0" : "=r" (res) ); \ |
67 | return res; \ |
||
68 | } |
||
27 | jermar | 69 | |
1780 | jermar | 70 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \ |
1074 | palkovsky | 71 | { \ |
72 | __asm__ volatile ("movl %0, %%" #reg : : "r" (regn)); \ |
||
73 | } |
||
38 | jermar | 74 | |
1074 | palkovsky | 75 | GEN_READ_REG(cr0); |
76 | GEN_READ_REG(cr2); |
||
77 | GEN_READ_REG(cr3); |
||
78 | GEN_WRITE_REG(cr3); |
||
115 | jermar | 79 | |
1074 | palkovsky | 80 | GEN_READ_REG(dr0); |
81 | GEN_READ_REG(dr1); |
||
82 | GEN_READ_REG(dr2); |
||
83 | GEN_READ_REG(dr3); |
||
84 | GEN_READ_REG(dr6); |
||
85 | GEN_READ_REG(dr7); |
||
86 | |||
87 | GEN_WRITE_REG(dr0); |
||
88 | GEN_WRITE_REG(dr1); |
||
89 | GEN_WRITE_REG(dr2); |
||
90 | GEN_WRITE_REG(dr3); |
||
91 | GEN_WRITE_REG(dr6); |
||
92 | GEN_WRITE_REG(dr7); |
||
93 | |||
352 | bondari | 94 | /** Byte to port |
95 | * |
||
96 | * Output byte to port |
||
97 | * |
||
98 | * @param port Port to write to |
||
99 | * @param val Value to write |
||
100 | */ |
||
1780 | jermar | 101 | static inline void outb(uint16_t port, uint8_t val) { __asm__ volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) ); } |
352 | bondari | 102 | |
353 | bondari | 103 | /** Word to port |
104 | * |
||
105 | * Output word to port |
||
106 | * |
||
107 | * @param port Port to write to |
||
108 | * @param val Value to write |
||
109 | */ |
||
1780 | jermar | 110 | static inline void outw(uint16_t port, uint16_t val) { __asm__ volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port) ); } |
352 | bondari | 111 | |
353 | bondari | 112 | /** Double word to port |
113 | * |
||
114 | * Output double word to port |
||
115 | * |
||
116 | * @param port Port to write to |
||
117 | * @param val Value to write |
||
118 | */ |
||
1780 | jermar | 119 | static inline void outl(uint16_t port, uint32_t val) { __asm__ volatile ("outl %l0, %w1\n" : : "a" (val), "d" (port) ); } |
353 | bondari | 120 | |
356 | bondari | 121 | /** Byte from port |
122 | * |
||
123 | * Get byte from port |
||
124 | * |
||
125 | * @param port Port to read from |
||
126 | * @return Value read |
||
127 | */ |
||
1780 | jermar | 128 | static inline uint8_t inb(uint16_t port) { uint8_t val; __asm__ volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) ); return val; } |
356 | bondari | 129 | |
130 | /** Word from port |
||
131 | * |
||
132 | * Get word from port |
||
133 | * |
||
134 | * @param port Port to read from |
||
135 | * @return Value read |
||
136 | */ |
||
1780 | jermar | 137 | static inline uint16_t inw(uint16_t port) { uint16_t val; __asm__ volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port) ); return val; } |
356 | bondari | 138 | |
139 | /** Double word from port |
||
140 | * |
||
141 | * Get double word from port |
||
142 | * |
||
143 | * @param port Port to read from |
||
144 | * @return Value read |
||
145 | */ |
||
1780 | jermar | 146 | static inline uint32_t inl(uint16_t port) { uint32_t val; __asm__ volatile ("inl %w1, %l0 \n" : "=a" (val) : "d" (port) ); return val; } |
356 | bondari | 147 | |
413 | jermar | 148 | /** Enable interrupts. |
115 | jermar | 149 | * |
150 | * Enable interrupts and return previous |
||
151 | * value of EFLAGS. |
||
413 | jermar | 152 | * |
153 | * @return Old interrupt priority level. |
||
115 | jermar | 154 | */ |
432 | jermar | 155 | static inline ipl_t interrupts_enable(void) |
156 | { |
||
413 | jermar | 157 | ipl_t v; |
115 | jermar | 158 | __asm__ volatile ( |
358 | bondari | 159 | "pushf\n\t" |
160 | "popl %0\n\t" |
||
115 | jermar | 161 | "sti\n" |
162 | : "=r" (v) |
||
163 | ); |
||
164 | return v; |
||
165 | } |
||
166 | |||
413 | jermar | 167 | /** Disable interrupts. |
115 | jermar | 168 | * |
169 | * Disable interrupts and return previous |
||
170 | * value of EFLAGS. |
||
413 | jermar | 171 | * |
172 | * @return Old interrupt priority level. |
||
115 | jermar | 173 | */ |
432 | jermar | 174 | static inline ipl_t interrupts_disable(void) |
175 | { |
||
413 | jermar | 176 | ipl_t v; |
115 | jermar | 177 | __asm__ volatile ( |
358 | bondari | 178 | "pushf\n\t" |
179 | "popl %0\n\t" |
||
115 | jermar | 180 | "cli\n" |
181 | : "=r" (v) |
||
182 | ); |
||
183 | return v; |
||
184 | } |
||
185 | |||
413 | jermar | 186 | /** Restore interrupt priority level. |
115 | jermar | 187 | * |
188 | * Restore EFLAGS. |
||
413 | jermar | 189 | * |
190 | * @param ipl Saved interrupt priority level. |
||
115 | jermar | 191 | */ |
432 | jermar | 192 | static inline void interrupts_restore(ipl_t ipl) |
193 | { |
||
115 | jermar | 194 | __asm__ volatile ( |
358 | bondari | 195 | "pushl %0\n\t" |
115 | jermar | 196 | "popf\n" |
413 | jermar | 197 | : : "r" (ipl) |
115 | jermar | 198 | ); |
199 | } |
||
200 | |||
413 | jermar | 201 | /** Return interrupt priority level. |
115 | jermar | 202 | * |
413 | jermar | 203 | * @return EFLAFS. |
115 | jermar | 204 | */ |
432 | jermar | 205 | static inline ipl_t interrupts_read(void) |
206 | { |
||
413 | jermar | 207 | ipl_t v; |
115 | jermar | 208 | __asm__ volatile ( |
358 | bondari | 209 | "pushf\n\t" |
115 | jermar | 210 | "popl %0\n" |
211 | : "=r" (v) |
||
212 | ); |
||
213 | return v; |
||
214 | } |
||
215 | |||
173 | jermar | 216 | /** Return base address of current stack |
217 | * |
||
218 | * Return the base address of the current stack. |
||
219 | * The stack is assumed to be STACK_SIZE bytes long. |
||
180 | jermar | 220 | * The stack must start on page boundary. |
173 | jermar | 221 | */ |
1780 | jermar | 222 | static inline uintptr_t get_stack_base(void) |
173 | jermar | 223 | { |
1780 | jermar | 224 | uintptr_t v; |
173 | jermar | 225 | |
226 | __asm__ volatile ("andl %%esp, %0\n" : "=r" (v) : "0" (~(STACK_SIZE-1))); |
||
227 | |||
228 | return v; |
||
229 | } |
||
230 | |||
581 | palkovsky | 231 | /** Return current IP address */ |
1780 | jermar | 232 | static inline uintptr_t * get_ip() |
581 | palkovsky | 233 | { |
1780 | jermar | 234 | uintptr_t *ip; |
581 | palkovsky | 235 | |
236 | __asm__ volatile ( |
||
237 | "mov %%eip, %0" |
||
238 | : "=r" (ip) |
||
239 | ); |
||
240 | return ip; |
||
241 | } |
||
242 | |||
597 | jermar | 243 | /** Invalidate TLB Entry. |
244 | * |
||
245 | * @param addr Address on a page whose TLB entry is to be invalidated. |
||
246 | */ |
||
1780 | jermar | 247 | static inline void invlpg(uintptr_t addr) |
597 | jermar | 248 | { |
1780 | jermar | 249 | __asm__ volatile ("invlpg %0\n" :: "m" (*(unative_t *)addr)); |
597 | jermar | 250 | } |
251 | |||
1186 | jermar | 252 | /** Load GDTR register from memory. |
253 | * |
||
254 | * @param gdtr_reg Address of memory from where to load GDTR. |
||
255 | */ |
||
1187 | jermar | 256 | static inline void gdtr_load(ptr_16_32_t *gdtr_reg) |
1186 | jermar | 257 | { |
1251 | jermar | 258 | __asm__ volatile ("lgdtl %0\n" : : "m" (*gdtr_reg)); |
1186 | jermar | 259 | } |
260 | |||
261 | /** Store GDTR register to memory. |
||
262 | * |
||
263 | * @param gdtr_reg Address of memory to where to load GDTR. |
||
264 | */ |
||
1187 | jermar | 265 | static inline void gdtr_store(ptr_16_32_t *gdtr_reg) |
1186 | jermar | 266 | { |
1251 | jermar | 267 | __asm__ volatile ("sgdtl %0\n" : : "m" (*gdtr_reg)); |
1186 | jermar | 268 | } |
269 | |||
270 | /** Load IDTR register from memory. |
||
271 | * |
||
272 | * @param idtr_reg Address of memory from where to load IDTR. |
||
273 | */ |
||
1187 | jermar | 274 | static inline void idtr_load(ptr_16_32_t *idtr_reg) |
1186 | jermar | 275 | { |
1251 | jermar | 276 | __asm__ volatile ("lidtl %0\n" : : "m" (*idtr_reg)); |
1186 | jermar | 277 | } |
278 | |||
279 | /** Load TR from descriptor table. |
||
280 | * |
||
281 | * @param sel Selector specifying descriptor of TSS segment. |
||
282 | */ |
||
1780 | jermar | 283 | static inline void tr_load(uint16_t sel) |
1186 | jermar | 284 | { |
285 | __asm__ volatile ("ltr %0" : : "r" (sel)); |
||
286 | } |
||
287 | |||
1 | jermar | 288 | #endif |
1702 | cejka | 289 | |
1888 | jermar | 290 | /** @} |
1702 | cejka | 291 | */ |