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