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

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

cstyle
(no change in functionality)

  • Property mode set to 100644
File size: 10.0 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>
[7a0359b]41#include <trace.h>
[b9e97fb]42
[c22531fc]43#define IO_SPACE_BOUNDARY ((void *) (64 * 1024))
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 */
[7a0359b]52NO_TRACE static 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)
[7a0359b]59 : "0" (~((uint64_t) STACK_SIZE - 1))
[f24d300]60 );
[db3341e]61
62 return v;
[361635c]63}
64
[7a0359b]65NO_TRACE static inline void cpu_sleep(void)
[6aea2e00]66{
[7a0359b]67 asm volatile (
68 "hlt\n"
69 );
[92d349c8]70}
[6aea2e00]71
[7a0359b]72NO_TRACE static inline void __attribute__((noreturn)) cpu_halt(void)
[6aea2e00]73{
[82474ef]74 while (true) {
75 asm volatile (
76 "hlt\n"
77 );
78 }
[92d349c8]79}
[fa0dfaf]80
[80d2bdb]81/** Byte from port
82 *
83 * Get byte from port
84 *
85 * @param port Port to read from
86 * @return Value read
[f24d300]87 *
[80d2bdb]88 */
[7a0359b]89NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
[92d349c8]90{
[abf04a54]91 if (port < (ioport8_t *) IO_SPACE_BOUNDARY) {
[667a4f8]92 uint8_t val;
[c22531fc]93
[667a4f8]94 asm volatile (
95 "inb %w[port], %b[val]\n"
96 : [val] "=a" (val)
97 : [port] "d" (port)
98 );
[c22531fc]99
[667a4f8]100 return val;
[c22531fc]101 } else
[667a4f8]102 return (uint8_t) *port;
[92d349c8]103}
[379d73f3]104
[9688513]105/** Word from port
106 *
107 * Get word from port
108 *
109 * @param port Port to read from
110 * @return Value read
[f24d300]111 *
[9688513]112 */
[7a0359b]113NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
[9688513]114{
[abf04a54]115 if (port < (ioport16_t *) IO_SPACE_BOUNDARY) {
[667a4f8]116 uint16_t val;
[c22531fc]117
[667a4f8]118 asm volatile (
119 "inw %w[port], %w[val]\n"
120 : [val] "=a" (val)
121 : [port] "d" (port)
122 );
[c22531fc]123
[667a4f8]124 return val;
[c22531fc]125 } else
[667a4f8]126 return (uint16_t) *port;
[9688513]127}
128
129/** Double word from port
130 *
131 * Get double word from port
132 *
133 * @param port Port to read from
134 * @return Value read
[f24d300]135 *
[9688513]136 */
[7a0359b]137NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
[9688513]138{
[abf04a54]139 if (port < (ioport32_t *) IO_SPACE_BOUNDARY) {
[667a4f8]140 uint32_t val;
[c22531fc]141
[667a4f8]142 asm volatile (
143 "inl %w[port], %[val]\n"
144 : [val] "=a" (val)
145 : [port] "d" (port)
146 );
[c22531fc]147
[667a4f8]148 return val;
[c22531fc]149 } else
[667a4f8]150 return (uint32_t) *port;
[9688513]151}
152
[80d2bdb]153/** Byte to port
154 *
155 * Output byte to port
156 *
157 * @param port Port to write to
158 * @param val Value to write
[f24d300]159 *
[80d2bdb]160 */
[7a0359b]161NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t val)
[92d349c8]162{
[abf04a54]163 if (port < (ioport8_t *) IO_SPACE_BOUNDARY) {
[667a4f8]164 asm volatile (
165 "outb %b[val], %w[port]\n"
166 :: [val] "a" (val), [port] "d" (port)
167 );
[c22531fc]168 } else
[abf04a54]169 *port = val;
[92d349c8]170}
[379d73f3]171
[9688513]172/** Word to port
173 *
174 * Output word to port
175 *
176 * @param port Port to write to
177 * @param val Value to write
[f24d300]178 *
[9688513]179 */
[7a0359b]180NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t val)
[9688513]181{
[abf04a54]182 if (port < (ioport16_t *) IO_SPACE_BOUNDARY) {
[667a4f8]183 asm volatile (
184 "outw %w[val], %w[port]\n"
185 :: [val] "a" (val), [port] "d" (port)
186 );
[c22531fc]187 } else
[abf04a54]188 *port = val;
[9688513]189}
190
191/** Double word to port
192 *
193 * Output double word to port
194 *
195 * @param port Port to write to
196 * @param val Value to write
[f24d300]197 *
[9688513]198 */
[7a0359b]199NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t val)
[9688513]200{
[abf04a54]201 if (port < (ioport32_t *) IO_SPACE_BOUNDARY) {
[667a4f8]202 asm volatile (
203 "outl %[val], %w[port]\n"
204 :: [val] "a" (val), [port] "d" (port)
205 );
[c22531fc]206 } else
[abf04a54]207 *port = val;
[9688513]208}
209
[37b451f7]210/** Swap Hidden part of GS register with visible one */
[7a0359b]211NO_TRACE static inline void swapgs(void)
[92d349c8]212{
[7a0359b]213 asm volatile (
214 "swapgs"
215 );
[92d349c8]216}
[37b451f7]217
[22f7769]218/** Enable interrupts.
[379d73f3]219 *
220 * Enable interrupts and return previous
221 * value of EFLAGS.
[22f7769]222 *
223 * @return Old interrupt priority level.
[f24d300]224 *
[379d73f3]225 */
[7a0359b]226NO_TRACE static inline ipl_t interrupts_enable(void) {
[22f7769]227 ipl_t v;
[f24d300]228
229 asm volatile (
[379d73f3]230 "pushfq\n"
[f24d300]231 "popq %[v]\n"
[379d73f3]232 "sti\n"
[f24d300]233 : [v] "=r" (v)
[379d73f3]234 );
[f24d300]235
[379d73f3]236 return v;
237}
238
[22f7769]239/** Disable interrupts.
[379d73f3]240 *
241 * Disable interrupts and return previous
242 * value of EFLAGS.
[22f7769]243 *
244 * @return Old interrupt priority level.
[f24d300]245 *
[379d73f3]246 */
[7a0359b]247NO_TRACE static inline ipl_t interrupts_disable(void) {
[22f7769]248 ipl_t v;
[f24d300]249
250 asm volatile (
[379d73f3]251 "pushfq\n"
[f24d300]252 "popq %[v]\n"
[379d73f3]253 "cli\n"
[f24d300]254 : [v] "=r" (v)
255 );
256
[379d73f3]257 return v;
258}
259
[22f7769]260/** Restore interrupt priority level.
[379d73f3]261 *
262 * Restore EFLAGS.
[22f7769]263 *
264 * @param ipl Saved interrupt priority level.
[f24d300]265 *
[379d73f3]266 */
[7a0359b]267NO_TRACE static inline void interrupts_restore(ipl_t ipl) {
[f24d300]268 asm volatile (
269 "pushq %[ipl]\n"
[379d73f3]270 "popfq\n"
[f24d300]271 :: [ipl] "r" (ipl)
272 );
[379d73f3]273}
274
[22f7769]275/** Return interrupt priority level.
[b9e97fb]276 *
277 * Return EFLAFS.
[22f7769]278 *
279 * @return Current interrupt priority level.
[f24d300]280 *
[b9e97fb]281 */
[7a0359b]282NO_TRACE static inline ipl_t interrupts_read(void) {
[22f7769]283 ipl_t v;
[f24d300]284
285 asm volatile (
[b9e97fb]286 "pushfq\n"
[f24d300]287 "popq %[v]\n"
288 : [v] "=r" (v)
[b9e97fb]289 );
[f24d300]290
[b9e97fb]291 return v;
292}
293
[d0ee0de]294/** Check interrupts state.
295 *
296 * @return True if interrupts are disabled.
297 *
298 */
[7a0359b]299NO_TRACE static inline bool interrupts_disabled(void)
[d0ee0de]300{
301 ipl_t v;
302
303 asm volatile (
304 "pushfq\n"
305 "popq %[v]\n"
306 : [v] "=r" (v)
307 );
308
309 return ((v & RFLAGS_IF) == 0);
310}
311
[dd4d6b0]312/** Write to MSR */
[7a0359b]313NO_TRACE static inline void write_msr(uint32_t msr, uint64_t value)
[dd4d6b0]314{
[f24d300]315 asm volatile (
316 "wrmsr\n"
317 :: "c" (msr),
318 "a" ((uint32_t) (value)),
319 "d" ((uint32_t) (value >> 32))
320 );
[dd4d6b0]321}
322
[96b02eb9]323NO_TRACE static inline sysarg_t read_msr(uint32_t msr)
[dd4d6b0]324{
[7f1c620]325 uint32_t ax, dx;
[f24d300]326
327 asm volatile (
328 "rdmsr\n"
329 : "=a" (ax), "=d" (dx)
330 : "c" (msr)
331 );
332
333 return ((uint64_t) dx << 32) | ax;
[dd4d6b0]334}
335
[ab08b42]336/** Enable local APIC
337 *
338 * Enable local APIC in MSR.
[f24d300]339 *
[ab08b42]340 */
[7a0359b]341NO_TRACE static inline void enable_l_apic_in_msr()
[ab08b42]342{
[f24d300]343 asm volatile (
[d6dcdd2e]344 "movl $0x1b, %%ecx\n"
345 "rdmsr\n"
[f24d300]346 "orl $(1 << 11),%%eax\n"
[d6dcdd2e]347 "orl $(0xfee00000),%%eax\n"
348 "wrmsr\n"
[7a0359b]349 ::: "%eax", "%ecx", "%edx"
[f24d300]350 );
[ab08b42]351}
352
[7910cff]353/** Invalidate TLB Entry.
354 *
355 * @param addr Address on a page whose TLB entry is to be invalidated.
[f24d300]356 *
[7910cff]357 */
[7a0359b]358NO_TRACE static inline void invlpg(uintptr_t addr)
[7910cff]359{
[f24d300]360 asm volatile (
361 "invlpg %[addr]\n"
[96b02eb9]362 :: [addr] "m" (*((sysarg_t *) addr))
[f24d300]363 );
[897ad60]364}
365
366/** Load GDTR register from memory.
367 *
368 * @param gdtr_reg Address of memory from where to load GDTR.
[f24d300]369 *
[897ad60]370 */
[7a0359b]371NO_TRACE static inline void gdtr_load(ptr_16_64_t *gdtr_reg)
[897ad60]372{
[f24d300]373 asm volatile (
374 "lgdtq %[gdtr_reg]\n"
375 :: [gdtr_reg] "m" (*gdtr_reg)
376 );
[897ad60]377}
378
379/** Store GDTR register to memory.
380 *
381 * @param gdtr_reg Address of memory to where to load GDTR.
[f24d300]382 *
[897ad60]383 */
[7a0359b]384NO_TRACE static inline void gdtr_store(ptr_16_64_t *gdtr_reg)
[897ad60]385{
[f24d300]386 asm volatile (
387 "sgdtq %[gdtr_reg]\n"
388 :: [gdtr_reg] "m" (*gdtr_reg)
389 );
[897ad60]390}
391
392/** Load IDTR register from memory.
393 *
394 * @param idtr_reg Address of memory from where to load IDTR.
[f24d300]395 *
[897ad60]396 */
[7a0359b]397NO_TRACE static inline void idtr_load(ptr_16_64_t *idtr_reg)
[897ad60]398{
[f24d300]399 asm volatile (
400 "lidtq %[idtr_reg]\n"
401 :: [idtr_reg] "m" (*idtr_reg));
[897ad60]402}
403
404/** Load TR from descriptor table.
405 *
406 * @param sel Selector specifying descriptor of TSS segment.
[f24d300]407 *
[897ad60]408 */
[7a0359b]409NO_TRACE static inline void tr_load(uint16_t sel)
[897ad60]410{
[f24d300]411 asm volatile (
412 "ltr %[sel]"
413 :: [sel] "r" (sel)
414 );
[7910cff]415}
[a3ac9a7]416
[96b02eb9]417#define GEN_READ_REG(reg) NO_TRACE static inline sysarg_t read_ ##reg (void) \
[f24d300]418 { \
[96b02eb9]419 sysarg_t res; \
[f24d300]420 asm volatile ( \
421 "movq %%" #reg ", %[res]" \
422 : [res] "=r" (res) \
423 ); \
424 return res; \
425 }
[4e49572]426
[96b02eb9]427#define GEN_WRITE_REG(reg) NO_TRACE static inline void write_ ##reg (sysarg_t regn) \
[f24d300]428 { \
429 asm volatile ( \
430 "movq %[regn], %%" #reg \
431 :: [regn] "r" (regn) \
432 ); \
433 }
[4e49572]434
[473e693]435GEN_READ_REG(cr0)
436GEN_READ_REG(cr2)
437GEN_READ_REG(cr3)
438GEN_WRITE_REG(cr3)
439
440GEN_READ_REG(dr0)
441GEN_READ_REG(dr1)
442GEN_READ_REG(dr2)
443GEN_READ_REG(dr3)
444GEN_READ_REG(dr6)
445GEN_READ_REG(dr7)
446
447GEN_WRITE_REG(dr0)
448GEN_WRITE_REG(dr1)
449GEN_WRITE_REG(dr2)
450GEN_WRITE_REG(dr3)
451GEN_WRITE_REG(dr6)
452GEN_WRITE_REG(dr7)
[4e49572]453
[7a0359b]454extern void asm_delay_loop(uint32_t);
455extern void asm_fake_loop(uint32_t);
456
[f77e591d]457extern uintptr_t int_0;
458extern uintptr_t int_1;
459extern uintptr_t int_2;
460extern uintptr_t int_3;
461extern uintptr_t int_4;
462extern uintptr_t int_5;
463extern uintptr_t int_6;
464extern uintptr_t int_7;
465extern uintptr_t int_8;
466extern uintptr_t int_9;
467extern uintptr_t int_10;
468extern uintptr_t int_11;
469extern uintptr_t int_12;
470extern uintptr_t int_13;
471extern uintptr_t int_14;
472extern uintptr_t int_15;
473extern uintptr_t int_16;
474extern uintptr_t int_17;
475extern uintptr_t int_18;
476extern uintptr_t int_19;
477extern uintptr_t int_20;
478extern uintptr_t int_21;
479extern uintptr_t int_22;
480extern uintptr_t int_23;
481extern uintptr_t int_24;
482extern uintptr_t int_25;
483extern uintptr_t int_26;
484extern uintptr_t int_27;
485extern uintptr_t int_28;
486extern uintptr_t int_29;
487extern uintptr_t int_30;
488extern uintptr_t int_31;
489extern uintptr_t int_32;
490extern uintptr_t int_33;
491extern uintptr_t int_34;
492extern uintptr_t int_35;
493extern uintptr_t int_36;
494extern uintptr_t int_37;
495extern uintptr_t int_38;
496extern uintptr_t int_39;
497extern uintptr_t int_40;
498extern uintptr_t int_41;
499extern uintptr_t int_42;
500extern uintptr_t int_43;
501extern uintptr_t int_44;
502extern uintptr_t int_45;
503extern uintptr_t int_46;
504extern uintptr_t int_47;
505extern uintptr_t int_48;
506extern uintptr_t int_49;
507extern uintptr_t int_50;
508extern uintptr_t int_51;
509extern uintptr_t int_52;
510extern uintptr_t int_53;
511extern uintptr_t int_54;
512extern uintptr_t int_55;
513extern uintptr_t int_56;
514extern uintptr_t int_57;
515extern uintptr_t int_58;
516extern uintptr_t int_59;
517extern uintptr_t int_60;
518extern uintptr_t int_61;
519extern uintptr_t int_62;
520extern uintptr_t int_63;
521
[361635c]522#endif
[b45c443]523
[06e1e95]524/** @}
[b45c443]525 */
Note: See TracBrowser for help on using the repository browser.