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