source: mainline/kernel/arch/amd64/include/asm.h@ d0ee0de

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d0ee0de was d0ee0de, checked in by Jakub Jermar <jakub@…>, 15 years ago

Add amd64 interrupts_disabled().

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