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

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

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

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