source: mainline/kernel/arch/ia32/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.3 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
[f761f1eb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[add04f7]30/** @addtogroup ia32
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[06e1e95]36#ifndef KERN_ia32_ASM_H_
37#define KERN_ia32_ASM_H_
[f761f1eb]38
[897ad60]39#include <arch/pm.h>
[2b4a9f26]40#include <arch/cpu.h>
[c22e964]41#include <typedefs.h>
[361635c]42#include <config.h>
[7a0359b]43#include <trace.h>
[f761f1eb]44
[18e0a6c]45/** Halt CPU
46 *
[3a1c048]47 * Halt the current CPU.
[add04f7]48 *
[18e0a6c]49 */
[7a0359b]50NO_TRACE static inline __attribute__((noreturn)) void cpu_halt(void)
[e7b7be3f]51{
[82474ef]52 while (true) {
53 asm volatile (
54 "hlt\n"
55 );
56 }
[60133d0]57}
[e7b7be3f]58
[7a0359b]59NO_TRACE static inline void cpu_sleep(void)
[e7b7be3f]60{
[7a0359b]61 asm volatile (
62 "hlt\n"
63 );
[60133d0]64}
[f761f1eb]65
[96b02eb9]66#define GEN_READ_REG(reg) NO_TRACE static inline sysarg_t read_ ##reg (void) \
[add04f7]67 { \
[96b02eb9]68 sysarg_t res; \
[add04f7]69 asm volatile ( \
70 "movl %%" #reg ", %[res]" \
71 : [res] "=r" (res) \
72 ); \
73 return res; \
74 }
[0f4e706]75
[96b02eb9]76#define GEN_WRITE_REG(reg) NO_TRACE static inline void write_ ##reg (sysarg_t regn) \
[add04f7]77 { \
78 asm volatile ( \
79 "movl %[regn], %%" #reg \
80 :: [regn] "r" (regn) \
81 ); \
82 }
[18e0a6c]83
[60133d0]84GEN_READ_REG(cr0)
85GEN_READ_REG(cr2)
86GEN_READ_REG(cr3)
87GEN_WRITE_REG(cr3)
88
89GEN_READ_REG(dr0)
90GEN_READ_REG(dr1)
91GEN_READ_REG(dr2)
92GEN_READ_REG(dr3)
93GEN_READ_REG(dr6)
94GEN_READ_REG(dr7)
95
96GEN_WRITE_REG(dr0)
97GEN_WRITE_REG(dr1)
98GEN_WRITE_REG(dr2)
99GEN_WRITE_REG(dr3)
100GEN_WRITE_REG(dr6)
101GEN_WRITE_REG(dr7)
[18e0a6c]102
[a5556b4]103/** Byte to port
104 *
105 * Output byte to port
106 *
107 * @param port Port to write to
108 * @param val Value to write
[add04f7]109 *
[a5556b4]110 */
[7a0359b]111NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t val)
[e7b7be3f]112{
[add04f7]113 asm volatile (
114 "outb %b[val], %w[port]\n"
[7a0359b]115 :: [val] "a" (val),
116 [port] "d" (port)
[add04f7]117 );
[e7b7be3f]118}
[a5556b4]119
[714675b]120/** Word to port
121 *
122 * Output word to port
123 *
124 * @param port Port to write to
125 * @param val Value to write
[add04f7]126 *
[714675b]127 */
[7a0359b]128NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t val)
[e7b7be3f]129{
[add04f7]130 asm volatile (
131 "outw %w[val], %w[port]\n"
[7a0359b]132 :: [val] "a" (val),
133 [port] "d" (port)
[add04f7]134 );
[e7b7be3f]135}
[714675b]136
137/** Double word to port
138 *
139 * Output double word to port
140 *
141 * @param port Port to write to
142 * @param val Value to write
[add04f7]143 *
[714675b]144 */
[7a0359b]145NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t val)
[e7b7be3f]146{
[add04f7]147 asm volatile (
148 "outl %[val], %w[port]\n"
[7a0359b]149 :: [val] "a" (val),
150 [port] "d" (port)
[add04f7]151 );
[e7b7be3f]152}
[a5556b4]153
[105a0dc]154/** Byte from port
155 *
156 * Get byte from port
157 *
158 * @param port Port to read from
159 * @return Value read
[add04f7]160 *
[105a0dc]161 */
[7a0359b]162NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
[e7b7be3f]163{
164 uint8_t val;
165
[add04f7]166 asm volatile (
167 "inb %w[port], %b[val]\n"
168 : [val] "=a" (val)
169 : [port] "d" (port)
170 );
171
[e7b7be3f]172 return val;
173}
[105a0dc]174
175/** Word from port
176 *
177 * Get word from port
178 *
179 * @param port Port to read from
180 * @return Value read
[add04f7]181 *
[105a0dc]182 */
[7a0359b]183NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
[e7b7be3f]184{
185 uint16_t val;
186
[add04f7]187 asm volatile (
188 "inw %w[port], %w[val]\n"
189 : [val] "=a" (val)
190 : [port] "d" (port)
191 );
192
[e7b7be3f]193 return val;
194}
[105a0dc]195
196/** Double word from port
197 *
198 * Get double word from port
199 *
200 * @param port Port to read from
201 * @return Value read
[add04f7]202 *
[105a0dc]203 */
[7a0359b]204NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
[e7b7be3f]205{
206 uint32_t val;
207
[add04f7]208 asm volatile (
209 "inl %w[port], %[val]\n"
210 : [val] "=a" (val)
211 : [port] "d" (port)
212 );
213
[e7b7be3f]214 return val;
215}
[105a0dc]216
[22f7769]217/** Enable interrupts.
[18e0a6c]218 *
219 * Enable interrupts and return previous
220 * value of EFLAGS.
[22f7769]221 *
222 * @return Old interrupt priority level.
[add04f7]223 *
[18e0a6c]224 */
[7a0359b]225NO_TRACE static inline ipl_t interrupts_enable(void)
[0259524]226{
[22f7769]227 ipl_t v;
[add04f7]228
[e7b7be3f]229 asm volatile (
[add04f7]230 "pushf\n"
231 "popl %[v]\n"
[18e0a6c]232 "sti\n"
[add04f7]233 : [v] "=r" (v)
[18e0a6c]234 );
[add04f7]235
[18e0a6c]236 return v;
237}
238
[22f7769]239/** Disable interrupts.
[18e0a6c]240 *
241 * Disable interrupts and return previous
242 * value of EFLAGS.
[22f7769]243 *
244 * @return Old interrupt priority level.
[add04f7]245 *
[18e0a6c]246 */
[7a0359b]247NO_TRACE static inline ipl_t interrupts_disable(void)
[0259524]248{
[22f7769]249 ipl_t v;
[add04f7]250
[e7b7be3f]251 asm volatile (
[add04f7]252 "pushf\n"
253 "popl %[v]\n"
[18e0a6c]254 "cli\n"
[add04f7]255 : [v] "=r" (v)
[18e0a6c]256 );
[add04f7]257
[18e0a6c]258 return v;
259}
260
[22f7769]261/** Restore interrupt priority level.
[18e0a6c]262 *
263 * Restore EFLAGS.
[22f7769]264 *
265 * @param ipl Saved interrupt priority level.
[add04f7]266 *
[18e0a6c]267 */
[7a0359b]268NO_TRACE static inline void interrupts_restore(ipl_t ipl)
[0259524]269{
[e7b7be3f]270 asm volatile (
[add04f7]271 "pushl %[ipl]\n"
[18e0a6c]272 "popf\n"
[add04f7]273 :: [ipl] "r" (ipl)
[18e0a6c]274 );
275}
276
[22f7769]277/** Return interrupt priority level.
[18e0a6c]278 *
[22f7769]279 * @return EFLAFS.
[add04f7]280 *
[18e0a6c]281 */
[7a0359b]282NO_TRACE static inline ipl_t interrupts_read(void)
[0259524]283{
[22f7769]284 ipl_t v;
[add04f7]285
[e7b7be3f]286 asm volatile (
[add04f7]287 "pushf\n"
288 "popl %[v]\n"
289 : [v] "=r" (v)
[18e0a6c]290 );
[add04f7]291
[18e0a6c]292 return v;
293}
[c9b8c5c]294
[2b4a9f26]295/** Check interrupts state.
296 *
297 * @return True if interrupts are disabled.
298 *
299 */
[7a0359b]300NO_TRACE static inline bool interrupts_disabled(void)
[2b4a9f26]301{
302 ipl_t v;
303
304 asm volatile (
305 "pushf\n"
306 "popl %[v]\n"
307 : [v] "=r" (v)
308 );
309
310 return ((v & EFLAGS_IF) == 0);
311}
312
[f2ef7fd]313/** Write to MSR */
[7a0359b]314NO_TRACE static inline void write_msr(uint32_t msr, uint64_t value)
[f2ef7fd]315{
[add04f7]316 asm volatile (
317 "wrmsr"
[7a0359b]318 :: "c" (msr),
319 "a" ((uint32_t) (value)),
[add04f7]320 "d" ((uint32_t) (value >> 32))
321 );
[f2ef7fd]322}
323
[7a0359b]324NO_TRACE static inline uint64_t read_msr(uint32_t msr)
[f2ef7fd]325{
326 uint32_t ax, dx;
[add04f7]327
328 asm volatile (
329 "rdmsr"
[7a0359b]330 : "=a" (ax),
331 "=d" (dx)
[add04f7]332 : "c" (msr)
333 );
334
335 return ((uint64_t) dx << 32) | ax;
[f2ef7fd]336}
337
338
[361635c]339/** Return base address of current stack
340 *
341 * Return the base address of the current stack.
342 * The stack is assumed to be STACK_SIZE bytes long.
[1fbbcd6]343 * The stack must start on page boundary.
[add04f7]344 *
[361635c]345 */
[7a0359b]346NO_TRACE static inline uintptr_t get_stack_base(void)
[361635c]347{
[7f1c620]348 uintptr_t v;
[361635c]349
[7f043c0]350 asm volatile (
[add04f7]351 "andl %%esp, %[v]\n"
352 : [v] "=r" (v)
[7f043c0]353 : "0" (~(STACK_SIZE - 1))
354 );
[361635c]355
356 return v;
357}
358
[7910cff]359/** Invalidate TLB Entry.
360 *
361 * @param addr Address on a page whose TLB entry is to be invalidated.
[add04f7]362 *
[7910cff]363 */
[7a0359b]364NO_TRACE static inline void invlpg(uintptr_t addr)
[7910cff]365{
[add04f7]366 asm volatile (
367 "invlpg %[addr]\n"
[96b02eb9]368 :: [addr] "m" (*(sysarg_t *) addr)
[add04f7]369 );
[7910cff]370}
371
[897ad60]372/** Load GDTR register from memory.
373 *
374 * @param gdtr_reg Address of memory from where to load GDTR.
[add04f7]375 *
[897ad60]376 */
[7a0359b]377NO_TRACE static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
[897ad60]378{
[add04f7]379 asm volatile (
380 "lgdtl %[gdtr_reg]\n"
381 :: [gdtr_reg] "m" (*gdtr_reg)
382 );
[897ad60]383}
384
385/** Store GDTR register to memory.
386 *
387 * @param gdtr_reg Address of memory to where to load GDTR.
[add04f7]388 *
[897ad60]389 */
[7a0359b]390NO_TRACE static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
[897ad60]391{
[add04f7]392 asm volatile (
393 "sgdtl %[gdtr_reg]\n"
[c4d11c5]394 : [gdtr_reg] "=m" (*gdtr_reg)
[add04f7]395 );
[897ad60]396}
397
398/** Load IDTR register from memory.
399 *
400 * @param idtr_reg Address of memory from where to load IDTR.
[add04f7]401 *
[897ad60]402 */
[7a0359b]403NO_TRACE static inline void idtr_load(ptr_16_32_t *idtr_reg)
[897ad60]404{
[add04f7]405 asm volatile (
406 "lidtl %[idtr_reg]\n"
407 :: [idtr_reg] "m" (*idtr_reg)
408 );
[897ad60]409}
410
411/** Load TR from descriptor table.
412 *
413 * @param sel Selector specifying descriptor of TSS segment.
[add04f7]414 *
[897ad60]415 */
[7a0359b]416NO_TRACE static inline void tr_load(uint16_t sel)
[897ad60]417{
[add04f7]418 asm volatile (
419 "ltr %[sel]"
420 :: [sel] "r" (sel)
421 );
[897ad60]422}
423
[7a0359b]424extern void paging_on(void);
425extern void enable_l_apic_in_msr(void);
426
427extern void asm_delay_loop(uint32_t);
428extern void asm_fake_loop(uint32_t);
429
[44c69b66]430extern uintptr_t int_syscall;
431
[b808660]432extern uintptr_t int_0;
433extern uintptr_t int_1;
434extern uintptr_t int_2;
435extern uintptr_t int_3;
436extern uintptr_t int_4;
437extern uintptr_t int_5;
438extern uintptr_t int_6;
439extern uintptr_t int_7;
440extern uintptr_t int_8;
441extern uintptr_t int_9;
442extern uintptr_t int_10;
443extern uintptr_t int_11;
444extern uintptr_t int_12;
445extern uintptr_t int_13;
446extern uintptr_t int_14;
447extern uintptr_t int_15;
448extern uintptr_t int_16;
449extern uintptr_t int_17;
450extern uintptr_t int_18;
451extern uintptr_t int_19;
452extern uintptr_t int_20;
453extern uintptr_t int_21;
454extern uintptr_t int_22;
455extern uintptr_t int_23;
456extern uintptr_t int_24;
457extern uintptr_t int_25;
458extern uintptr_t int_26;
459extern uintptr_t int_27;
460extern uintptr_t int_28;
461extern uintptr_t int_29;
462extern uintptr_t int_30;
463extern uintptr_t int_31;
464extern uintptr_t int_32;
465extern uintptr_t int_33;
466extern uintptr_t int_34;
467extern uintptr_t int_35;
468extern uintptr_t int_36;
469extern uintptr_t int_37;
470extern uintptr_t int_38;
471extern uintptr_t int_39;
472extern uintptr_t int_40;
473extern uintptr_t int_41;
474extern uintptr_t int_42;
475extern uintptr_t int_43;
476extern uintptr_t int_44;
477extern uintptr_t int_45;
478extern uintptr_t int_46;
479extern uintptr_t int_47;
480extern uintptr_t int_48;
481extern uintptr_t int_49;
482extern uintptr_t int_50;
483extern uintptr_t int_51;
484extern uintptr_t int_52;
485extern uintptr_t int_53;
486extern uintptr_t int_54;
487extern uintptr_t int_55;
488extern uintptr_t int_56;
489extern uintptr_t int_57;
490extern uintptr_t int_58;
491extern uintptr_t int_59;
492extern uintptr_t int_60;
493extern uintptr_t int_61;
494extern uintptr_t int_62;
495extern uintptr_t int_63;
496
[f761f1eb]497#endif
[b45c443]498
[06e1e95]499/** @}
[b45c443]500 */
Note: See TracBrowser for help on using the repository browser.