source: mainline/kernel/arch/ia32/include/asm.h@ 7bcfbbc

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

asm volatile → asm volatile

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
[f761f1eb]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
[06e1e95]30/** @addtogroup ia32
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[06e1e95]36#ifndef KERN_ia32_ASM_H_
37#define KERN_ia32_ASM_H_
[f761f1eb]38
[897ad60]39#include <arch/pm.h>
[f761f1eb]40#include <arch/types.h>
[361635c]41#include <config.h>
[f761f1eb]42
[7f1c620]43extern uint32_t interrupt_handler_size;
[f761f1eb]44
45extern void paging_on(void);
46
47extern void interrupt_handlers(void);
48
49extern void enable_l_apic_in_msr(void);
50
[9c0a9b3]51
[7f1c620]52extern void asm_delay_loop(uint32_t t);
53extern void asm_fake_loop(uint32_t t);
[9c0a9b3]54
55
[18e0a6c]56/** Halt CPU
57 *
58 * Halt the current CPU until interrupt event.
59 */
[e7b7be3f]60static inline void cpu_halt(void)
61{
62 asm("hlt\n");
63};
64
65static inline void cpu_sleep(void)
66{
67 asm("hlt\n");
68};
[f761f1eb]69
[7f1c620]70#define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
[23d22eb]71 { \
[7f1c620]72 unative_t res; \
[e7b7be3f]73 asm volatile ("movl %%" #reg ", %0" : "=r" (res) ); \
[23d22eb]74 return res; \
75 }
[0f4e706]76
[7f1c620]77#define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
[23d22eb]78 { \
[e7b7be3f]79 asm volatile ("movl %0, %%" #reg : : "r" (regn)); \
[23d22eb]80 }
[18e0a6c]81
[23d22eb]82GEN_READ_REG(cr0);
83GEN_READ_REG(cr2);
84GEN_READ_REG(cr3);
85GEN_WRITE_REG(cr3);
86
87GEN_READ_REG(dr0);
88GEN_READ_REG(dr1);
89GEN_READ_REG(dr2);
90GEN_READ_REG(dr3);
91GEN_READ_REG(dr6);
92GEN_READ_REG(dr7);
93
94GEN_WRITE_REG(dr0);
95GEN_WRITE_REG(dr1);
96GEN_WRITE_REG(dr2);
97GEN_WRITE_REG(dr3);
98GEN_WRITE_REG(dr6);
99GEN_WRITE_REG(dr7);
[18e0a6c]100
[a5556b4]101/** Byte to port
102 *
103 * Output byte to port
104 *
105 * @param port Port to write to
106 * @param val Value to write
107 */
[e7b7be3f]108static inline void outb(uint16_t port, uint8_t val)
109{
110 asm volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) );
111}
[a5556b4]112
[714675b]113/** Word to port
114 *
115 * Output word to port
116 *
117 * @param port Port to write to
118 * @param val Value to write
119 */
[e7b7be3f]120static inline void outw(uint16_t port, uint16_t val)
121{
122 asm volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port) );
123}
[714675b]124
125/** Double word to port
126 *
127 * Output double word to port
128 *
129 * @param port Port to write to
130 * @param val Value to write
131 */
[e7b7be3f]132static inline void outl(uint16_t port, uint32_t val)
133{
134 asm volatile ("outl %l0, %w1\n" : : "a" (val), "d" (port) );
135}
[a5556b4]136
[105a0dc]137/** Byte from port
138 *
139 * Get byte from port
140 *
141 * @param port Port to read from
142 * @return Value read
143 */
[e7b7be3f]144static inline uint8_t inb(uint16_t port)
145{
146 uint8_t val;
147
148 asm volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) );
149 return val;
150}
[105a0dc]151
152/** Word from port
153 *
154 * Get word from port
155 *
156 * @param port Port to read from
157 * @return Value read
158 */
[e7b7be3f]159static inline uint16_t inw(uint16_t port)
160{
161 uint16_t val;
162
163 asm volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port) );
164 return val;
165}
[105a0dc]166
167/** Double word from port
168 *
169 * Get double word from port
170 *
171 * @param port Port to read from
172 * @return Value read
173 */
[e7b7be3f]174static inline uint32_t inl(uint16_t port)
175{
176 uint32_t val;
177
178 asm volatile ("inl %w1, %l0 \n" : "=a" (val) : "d" (port) );
179 return val;
180}
[105a0dc]181
[22f7769]182/** Enable interrupts.
[18e0a6c]183 *
184 * Enable interrupts and return previous
185 * value of EFLAGS.
[22f7769]186 *
187 * @return Old interrupt priority level.
[18e0a6c]188 */
[0259524]189static inline ipl_t interrupts_enable(void)
190{
[22f7769]191 ipl_t v;
[e7b7be3f]192 asm volatile (
[104dc0b]193 "pushf\n\t"
194 "popl %0\n\t"
[18e0a6c]195 "sti\n"
196 : "=r" (v)
197 );
198 return v;
199}
200
[22f7769]201/** Disable interrupts.
[18e0a6c]202 *
203 * Disable interrupts and return previous
204 * value of EFLAGS.
[22f7769]205 *
206 * @return Old interrupt priority level.
[18e0a6c]207 */
[0259524]208static inline ipl_t interrupts_disable(void)
209{
[22f7769]210 ipl_t v;
[e7b7be3f]211 asm volatile (
[104dc0b]212 "pushf\n\t"
213 "popl %0\n\t"
[18e0a6c]214 "cli\n"
215 : "=r" (v)
216 );
217 return v;
218}
219
[22f7769]220/** Restore interrupt priority level.
[18e0a6c]221 *
222 * Restore EFLAGS.
[22f7769]223 *
224 * @param ipl Saved interrupt priority level.
[18e0a6c]225 */
[0259524]226static inline void interrupts_restore(ipl_t ipl)
227{
[e7b7be3f]228 asm volatile (
[104dc0b]229 "pushl %0\n\t"
[18e0a6c]230 "popf\n"
[22f7769]231 : : "r" (ipl)
[18e0a6c]232 );
233}
234
[22f7769]235/** Return interrupt priority level.
[18e0a6c]236 *
[22f7769]237 * @return EFLAFS.
[18e0a6c]238 */
[0259524]239static inline ipl_t interrupts_read(void)
240{
[22f7769]241 ipl_t v;
[e7b7be3f]242 asm volatile (
[104dc0b]243 "pushf\n\t"
[18e0a6c]244 "popl %0\n"
245 : "=r" (v)
246 );
247 return v;
248}
[c9b8c5c]249
[361635c]250/** Return base address of current stack
251 *
252 * Return the base address of the current stack.
253 * The stack is assumed to be STACK_SIZE bytes long.
[1fbbcd6]254 * The stack must start on page boundary.
[361635c]255 */
[7f1c620]256static inline uintptr_t get_stack_base(void)
[361635c]257{
[7f1c620]258 uintptr_t v;
[361635c]259
[e7b7be3f]260 asm volatile ("andl %%esp, %0\n" : "=r" (v) : "0" (~(STACK_SIZE-1)));
[361635c]261
262 return v;
263}
264
[a3ac9a7]265/** Return current IP address */
[7f1c620]266static inline uintptr_t * get_ip()
[a3ac9a7]267{
[7f1c620]268 uintptr_t *ip;
[a3ac9a7]269
[e7b7be3f]270 asm volatile (
[a3ac9a7]271 "mov %%eip, %0"
272 : "=r" (ip)
273 );
274 return ip;
275}
276
[7910cff]277/** Invalidate TLB Entry.
278 *
279 * @param addr Address on a page whose TLB entry is to be invalidated.
280 */
[7f1c620]281static inline void invlpg(uintptr_t addr)
[7910cff]282{
[e7b7be3f]283 asm volatile ("invlpg %0\n" :: "m" (*(unative_t *)addr));
[7910cff]284}
285
[897ad60]286/** Load GDTR register from memory.
287 *
288 * @param gdtr_reg Address of memory from where to load GDTR.
289 */
[39cea6a]290static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
[897ad60]291{
[e7b7be3f]292 asm volatile ("lgdtl %0\n" : : "m" (*gdtr_reg));
[897ad60]293}
294
295/** Store GDTR register to memory.
296 *
297 * @param gdtr_reg Address of memory to where to load GDTR.
298 */
[39cea6a]299static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
[897ad60]300{
[e7b7be3f]301 asm volatile ("sgdtl %0\n" : : "m" (*gdtr_reg));
[897ad60]302}
303
304/** Load IDTR register from memory.
305 *
306 * @param idtr_reg Address of memory from where to load IDTR.
307 */
[39cea6a]308static inline void idtr_load(ptr_16_32_t *idtr_reg)
[897ad60]309{
[e7b7be3f]310 asm volatile ("lidtl %0\n" : : "m" (*idtr_reg));
[897ad60]311}
312
313/** Load TR from descriptor table.
314 *
315 * @param sel Selector specifying descriptor of TSS segment.
316 */
[7f1c620]317static inline void tr_load(uint16_t sel)
[897ad60]318{
[e7b7be3f]319 asm volatile ("ltr %0" : : "r" (sel));
[897ad60]320}
321
[f761f1eb]322#endif
[b45c443]323
[06e1e95]324/** @}
[b45c443]325 */
Note: See TracBrowser for help on using the repository browser.