source: mainline/kernel/arch/ia32/include/asm.h@ 3d6beaa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3d6beaa was 3d6beaa, checked in by Martin Decky <martin@…>, 15 years ago

get rid of get_ip()

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
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
30/** @addtogroup ia32
31 * @{
32 */
33/** @file
34 */
35
36#ifndef KERN_ia32_ASM_H_
37#define KERN_ia32_ASM_H_
38
39#include <arch/pm.h>
40#include <arch/cpu.h>
41#include <typedefs.h>
42#include <config.h>
43
44extern uint32_t interrupt_handler_size;
45
46extern void paging_on(void);
47
48extern void interrupt_handlers(void);
49
50extern void enable_l_apic_in_msr(void);
51
52
53extern void asm_delay_loop(uint32_t t);
54extern void asm_fake_loop(uint32_t t);
55
56
57/** Halt CPU
58 *
59 * Halt the current CPU.
60 *
61 */
62static inline __attribute__((noreturn)) void cpu_halt(void)
63{
64 while (true) {
65 asm volatile (
66 "hlt\n"
67 );
68 }
69}
70
71static inline void cpu_sleep(void)
72{
73 asm volatile ("hlt\n");
74}
75
76#define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
77 { \
78 unative_t res; \
79 asm volatile ( \
80 "movl %%" #reg ", %[res]" \
81 : [res] "=r" (res) \
82 ); \
83 return res; \
84 }
85
86#define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
87 { \
88 asm volatile ( \
89 "movl %[regn], %%" #reg \
90 :: [regn] "r" (regn) \
91 ); \
92 }
93
94GEN_READ_REG(cr0)
95GEN_READ_REG(cr2)
96GEN_READ_REG(cr3)
97GEN_WRITE_REG(cr3)
98
99GEN_READ_REG(dr0)
100GEN_READ_REG(dr1)
101GEN_READ_REG(dr2)
102GEN_READ_REG(dr3)
103GEN_READ_REG(dr6)
104GEN_READ_REG(dr7)
105
106GEN_WRITE_REG(dr0)
107GEN_WRITE_REG(dr1)
108GEN_WRITE_REG(dr2)
109GEN_WRITE_REG(dr3)
110GEN_WRITE_REG(dr6)
111GEN_WRITE_REG(dr7)
112
113/** Byte to port
114 *
115 * Output byte to port
116 *
117 * @param port Port to write to
118 * @param val Value to write
119 *
120 */
121static inline void pio_write_8(ioport8_t *port, uint8_t val)
122{
123 asm volatile (
124 "outb %b[val], %w[port]\n"
125 :: [val] "a" (val), [port] "d" (port)
126 );
127}
128
129/** Word to port
130 *
131 * Output word to port
132 *
133 * @param port Port to write to
134 * @param val Value to write
135 *
136 */
137static inline void pio_write_16(ioport16_t *port, uint16_t val)
138{
139 asm volatile (
140 "outw %w[val], %w[port]\n"
141 :: [val] "a" (val), [port] "d" (port)
142 );
143}
144
145/** Double word to port
146 *
147 * Output double word to port
148 *
149 * @param port Port to write to
150 * @param val Value to write
151 *
152 */
153static inline void pio_write_32(ioport32_t *port, uint32_t val)
154{
155 asm volatile (
156 "outl %[val], %w[port]\n"
157 :: [val] "a" (val), [port] "d" (port)
158 );
159}
160
161/** Byte from port
162 *
163 * Get byte from port
164 *
165 * @param port Port to read from
166 * @return Value read
167 *
168 */
169static inline uint8_t pio_read_8(ioport8_t *port)
170{
171 uint8_t val;
172
173 asm volatile (
174 "inb %w[port], %b[val]\n"
175 : [val] "=a" (val)
176 : [port] "d" (port)
177 );
178
179 return val;
180}
181
182/** Word from port
183 *
184 * Get word from port
185 *
186 * @param port Port to read from
187 * @return Value read
188 *
189 */
190static inline uint16_t pio_read_16(ioport16_t *port)
191{
192 uint16_t val;
193
194 asm volatile (
195 "inw %w[port], %w[val]\n"
196 : [val] "=a" (val)
197 : [port] "d" (port)
198 );
199
200 return val;
201}
202
203/** Double word from port
204 *
205 * Get double word from port
206 *
207 * @param port Port to read from
208 * @return Value read
209 *
210 */
211static inline uint32_t pio_read_32(ioport32_t *port)
212{
213 uint32_t val;
214
215 asm volatile (
216 "inl %w[port], %[val]\n"
217 : [val] "=a" (val)
218 : [port] "d" (port)
219 );
220
221 return val;
222}
223
224/** Enable interrupts.
225 *
226 * Enable interrupts and return previous
227 * value of EFLAGS.
228 *
229 * @return Old interrupt priority level.
230 *
231 */
232static inline ipl_t interrupts_enable(void)
233{
234 ipl_t v;
235
236 asm volatile (
237 "pushf\n"
238 "popl %[v]\n"
239 "sti\n"
240 : [v] "=r" (v)
241 );
242
243 return v;
244}
245
246/** Disable interrupts.
247 *
248 * Disable interrupts and return previous
249 * value of EFLAGS.
250 *
251 * @return Old interrupt priority level.
252 *
253 */
254static inline ipl_t interrupts_disable(void)
255{
256 ipl_t v;
257
258 asm volatile (
259 "pushf\n"
260 "popl %[v]\n"
261 "cli\n"
262 : [v] "=r" (v)
263 );
264
265 return v;
266}
267
268/** Restore interrupt priority level.
269 *
270 * Restore EFLAGS.
271 *
272 * @param ipl Saved interrupt priority level.
273 *
274 */
275static inline void interrupts_restore(ipl_t ipl)
276{
277 asm volatile (
278 "pushl %[ipl]\n"
279 "popf\n"
280 :: [ipl] "r" (ipl)
281 );
282}
283
284/** Return interrupt priority level.
285 *
286 * @return EFLAFS.
287 *
288 */
289static inline ipl_t interrupts_read(void)
290{
291 ipl_t v;
292
293 asm volatile (
294 "pushf\n"
295 "popl %[v]\n"
296 : [v] "=r" (v)
297 );
298
299 return v;
300}
301
302/** Check interrupts state.
303 *
304 * @return True if interrupts are disabled.
305 *
306 */
307static inline bool interrupts_disabled(void)
308{
309 ipl_t v;
310
311 asm volatile (
312 "pushf\n"
313 "popl %[v]\n"
314 : [v] "=r" (v)
315 );
316
317 return ((v & EFLAGS_IF) == 0);
318}
319
320/** Write to MSR */
321static inline void write_msr(uint32_t msr, uint64_t value)
322{
323 asm volatile (
324 "wrmsr"
325 :: "c" (msr), "a" ((uint32_t) (value)),
326 "d" ((uint32_t) (value >> 32))
327 );
328}
329
330static inline uint64_t read_msr(uint32_t msr)
331{
332 uint32_t ax, dx;
333
334 asm volatile (
335 "rdmsr"
336 : "=a" (ax), "=d" (dx)
337 : "c" (msr)
338 );
339
340 return ((uint64_t) dx << 32) | ax;
341}
342
343
344/** Return base address of current stack
345 *
346 * Return the base address of the current stack.
347 * The stack is assumed to be STACK_SIZE bytes long.
348 * The stack must start on page boundary.
349 *
350 */
351static inline uintptr_t get_stack_base(void)
352{
353 uintptr_t v;
354
355 asm volatile (
356 "andl %%esp, %[v]\n"
357 : [v] "=r" (v)
358 : "0" (~(STACK_SIZE - 1))
359 );
360
361 return v;
362}
363
364/** Invalidate TLB Entry.
365 *
366 * @param addr Address on a page whose TLB entry is to be invalidated.
367 *
368 */
369static inline void invlpg(uintptr_t addr)
370{
371 asm volatile (
372 "invlpg %[addr]\n"
373 :: [addr] "m" (*(unative_t *) addr)
374 );
375}
376
377/** Load GDTR register from memory.
378 *
379 * @param gdtr_reg Address of memory from where to load GDTR.
380 *
381 */
382static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
383{
384 asm volatile (
385 "lgdtl %[gdtr_reg]\n"
386 :: [gdtr_reg] "m" (*gdtr_reg)
387 );
388}
389
390/** Store GDTR register to memory.
391 *
392 * @param gdtr_reg Address of memory to where to load GDTR.
393 *
394 */
395static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
396{
397 asm volatile (
398 "sgdtl %[gdtr_reg]\n"
399 : [gdtr_reg] "=m" (*gdtr_reg)
400 );
401}
402
403/** Load IDTR register from memory.
404 *
405 * @param idtr_reg Address of memory from where to load IDTR.
406 *
407 */
408static inline void idtr_load(ptr_16_32_t *idtr_reg)
409{
410 asm volatile (
411 "lidtl %[idtr_reg]\n"
412 :: [idtr_reg] "m" (*idtr_reg)
413 );
414}
415
416/** Load TR from descriptor table.
417 *
418 * @param sel Selector specifying descriptor of TSS segment.
419 *
420 */
421static inline void tr_load(uint16_t sel)
422{
423 asm volatile (
424 "ltr %[sel]"
425 :: [sel] "r" (sel)
426 );
427}
428
429#endif
430
431/** @}
432 */
Note: See TracBrowser for help on using the repository browser.