source: mainline/kernel/arch/ia32/include/asm.h@ 1b1164e8

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

use [u]int{8|16|32|64}_t type definitions as detected by the autotool
replace direct usage of arch/types.h with typedefs.h

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