source: mainline/kernel/arch/ia32/include/asm.h@ 667a4f8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 667a4f8 was 667a4f8, checked in by Zdenek Bouska <zdenek.bouska@…>, 14 years ago

Memory mapped registers accessible by pio_* frunctions (ia32, amd64)

  • Property mode set to 100644
File size: 9.8 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
[667a4f8]103#define IO_SPACE_BOUNDARY ((void *) (64 * 1024))
104
[a5556b4]105/** Byte to port
106 *
107 * Output byte to port
108 *
109 * @param port Port to write to
110 * @param val Value to write
[add04f7]111 *
[a5556b4]112 */
[7a0359b]113NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t val)
[e7b7be3f]114{
[667a4f8]115 if (((void *)port) < IO_SPACE_BOUNDARY) {
116 asm volatile (
117 "outb %b[val], %w[port]\n"
118 :: [val] "a" (val), [port] "d" (port)
119 );
120 } else {
121 *((uint8_t *) port) = val;
122 }
[e7b7be3f]123}
[a5556b4]124
[714675b]125/** Word to port
126 *
127 * Output word to port
128 *
129 * @param port Port to write to
130 * @param val Value to write
[add04f7]131 *
[714675b]132 */
[7a0359b]133NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t val)
[e7b7be3f]134{
[667a4f8]135 if (((void *)port) < IO_SPACE_BOUNDARY) {
136 asm volatile (
137 "outw %w[val], %w[port]\n"
138 :: [val] "a" (val), [port] "d" (port)
139 );
140 } else {
141 *((uint16_t *) port) = val;
142 }
[e7b7be3f]143}
[714675b]144
145/** Double word to port
146 *
147 * Output double word to port
148 *
149 * @param port Port to write to
150 * @param val Value to write
[add04f7]151 *
[714675b]152 */
[7a0359b]153NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t val)
[e7b7be3f]154{
[667a4f8]155 if (((void *)port) < IO_SPACE_BOUNDARY) {
156 asm volatile (
157 "outl %[val], %w[port]\n"
158 :: [val] "a" (val), [port] "d" (port)
159 );
160 } else {
161 *((uint32_t *) port) = val;
162 }
[e7b7be3f]163}
[a5556b4]164
[105a0dc]165/** Byte from port
166 *
167 * Get byte from port
168 *
169 * @param port Port to read from
170 * @return Value read
[add04f7]171 *
[105a0dc]172 */
[7a0359b]173NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
[e7b7be3f]174{
[667a4f8]175 if (((void *)port) < IO_SPACE_BOUNDARY) {
176 uint8_t val;
177 asm volatile (
178 "inb %w[port], %b[val]\n"
179 : [val] "=a" (val)
180 : [port] "d" (port)
181 );
182 return val;
183 } else {
184 return (uint8_t) *port;
185 }
[e7b7be3f]186}
[105a0dc]187
188/** Word from port
189 *
190 * Get word from port
191 *
192 * @param port Port to read from
193 * @return Value read
[add04f7]194 *
[105a0dc]195 */
[7a0359b]196NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
[e7b7be3f]197{
[667a4f8]198 if (((void *)port) < IO_SPACE_BOUNDARY) {
199 uint16_t val;
200 asm volatile (
201 "inw %w[port], %w[val]\n"
202 : [val] "=a" (val)
203 : [port] "d" (port)
204 );
205 return val;
206 } else {
207 return (uint16_t) *port;
208 }
[e7b7be3f]209}
[105a0dc]210
211/** Double word from port
212 *
213 * Get double word from port
214 *
215 * @param port Port to read from
216 * @return Value read
[add04f7]217 *
[105a0dc]218 */
[7a0359b]219NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
[e7b7be3f]220{
[667a4f8]221 if (((void *)port) < IO_SPACE_BOUNDARY) {
222 uint32_t val;
223 asm volatile (
224 "inl %w[port], %[val]\n"
225 : [val] "=a" (val)
226 : [port] "d" (port)
227 );
228 return val;
229 } else {
230 return (uint32_t) *port;
231 }
[e7b7be3f]232}
[105a0dc]233
[22f7769]234/** Enable interrupts.
[18e0a6c]235 *
236 * Enable interrupts and return previous
237 * value of EFLAGS.
[22f7769]238 *
239 * @return Old interrupt priority level.
[add04f7]240 *
[18e0a6c]241 */
[7a0359b]242NO_TRACE static inline ipl_t interrupts_enable(void)
[0259524]243{
[22f7769]244 ipl_t v;
[add04f7]245
[e7b7be3f]246 asm volatile (
[add04f7]247 "pushf\n"
248 "popl %[v]\n"
[18e0a6c]249 "sti\n"
[add04f7]250 : [v] "=r" (v)
[18e0a6c]251 );
[add04f7]252
[18e0a6c]253 return v;
254}
255
[22f7769]256/** Disable interrupts.
[18e0a6c]257 *
258 * Disable interrupts and return previous
259 * value of EFLAGS.
[22f7769]260 *
261 * @return Old interrupt priority level.
[add04f7]262 *
[18e0a6c]263 */
[7a0359b]264NO_TRACE static inline ipl_t interrupts_disable(void)
[0259524]265{
[22f7769]266 ipl_t v;
[add04f7]267
[e7b7be3f]268 asm volatile (
[add04f7]269 "pushf\n"
270 "popl %[v]\n"
[18e0a6c]271 "cli\n"
[add04f7]272 : [v] "=r" (v)
[18e0a6c]273 );
[add04f7]274
[18e0a6c]275 return v;
276}
277
[22f7769]278/** Restore interrupt priority level.
[18e0a6c]279 *
280 * Restore EFLAGS.
[22f7769]281 *
282 * @param ipl Saved interrupt priority level.
[add04f7]283 *
[18e0a6c]284 */
[7a0359b]285NO_TRACE static inline void interrupts_restore(ipl_t ipl)
[0259524]286{
[e7b7be3f]287 asm volatile (
[add04f7]288 "pushl %[ipl]\n"
[18e0a6c]289 "popf\n"
[add04f7]290 :: [ipl] "r" (ipl)
[18e0a6c]291 );
292}
293
[22f7769]294/** Return interrupt priority level.
[18e0a6c]295 *
[22f7769]296 * @return EFLAFS.
[add04f7]297 *
[18e0a6c]298 */
[7a0359b]299NO_TRACE static inline ipl_t interrupts_read(void)
[0259524]300{
[22f7769]301 ipl_t v;
[add04f7]302
[e7b7be3f]303 asm volatile (
[add04f7]304 "pushf\n"
305 "popl %[v]\n"
306 : [v] "=r" (v)
[18e0a6c]307 );
[add04f7]308
[18e0a6c]309 return v;
310}
[c9b8c5c]311
[2b4a9f26]312/** Check interrupts state.
313 *
314 * @return True if interrupts are disabled.
315 *
316 */
[7a0359b]317NO_TRACE static inline bool interrupts_disabled(void)
[2b4a9f26]318{
319 ipl_t v;
320
321 asm volatile (
322 "pushf\n"
323 "popl %[v]\n"
324 : [v] "=r" (v)
325 );
326
327 return ((v & EFLAGS_IF) == 0);
328}
329
[f2ef7fd]330/** Write to MSR */
[7a0359b]331NO_TRACE static inline void write_msr(uint32_t msr, uint64_t value)
[f2ef7fd]332{
[add04f7]333 asm volatile (
334 "wrmsr"
[7a0359b]335 :: "c" (msr),
336 "a" ((uint32_t) (value)),
[add04f7]337 "d" ((uint32_t) (value >> 32))
338 );
[f2ef7fd]339}
340
[7a0359b]341NO_TRACE static inline uint64_t read_msr(uint32_t msr)
[f2ef7fd]342{
343 uint32_t ax, dx;
[add04f7]344
345 asm volatile (
346 "rdmsr"
[7a0359b]347 : "=a" (ax),
348 "=d" (dx)
[add04f7]349 : "c" (msr)
350 );
351
352 return ((uint64_t) dx << 32) | ax;
[f2ef7fd]353}
354
355
[361635c]356/** Return base address of current stack
357 *
358 * Return the base address of the current stack.
359 * The stack is assumed to be STACK_SIZE bytes long.
[1fbbcd6]360 * The stack must start on page boundary.
[add04f7]361 *
[361635c]362 */
[7a0359b]363NO_TRACE static inline uintptr_t get_stack_base(void)
[361635c]364{
[7f1c620]365 uintptr_t v;
[361635c]366
[7f043c0]367 asm volatile (
[add04f7]368 "andl %%esp, %[v]\n"
369 : [v] "=r" (v)
[7f043c0]370 : "0" (~(STACK_SIZE - 1))
371 );
[361635c]372
373 return v;
374}
375
[7910cff]376/** Invalidate TLB Entry.
377 *
378 * @param addr Address on a page whose TLB entry is to be invalidated.
[add04f7]379 *
[7910cff]380 */
[7a0359b]381NO_TRACE static inline void invlpg(uintptr_t addr)
[7910cff]382{
[add04f7]383 asm volatile (
384 "invlpg %[addr]\n"
[96b02eb9]385 :: [addr] "m" (*(sysarg_t *) addr)
[add04f7]386 );
[7910cff]387}
388
[897ad60]389/** Load GDTR register from memory.
390 *
391 * @param gdtr_reg Address of memory from where to load GDTR.
[add04f7]392 *
[897ad60]393 */
[7a0359b]394NO_TRACE static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
[897ad60]395{
[add04f7]396 asm volatile (
397 "lgdtl %[gdtr_reg]\n"
398 :: [gdtr_reg] "m" (*gdtr_reg)
399 );
[897ad60]400}
401
402/** Store GDTR register to memory.
403 *
404 * @param gdtr_reg Address of memory to where to load GDTR.
[add04f7]405 *
[897ad60]406 */
[7a0359b]407NO_TRACE static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
[897ad60]408{
[add04f7]409 asm volatile (
410 "sgdtl %[gdtr_reg]\n"
[c4d11c5]411 : [gdtr_reg] "=m" (*gdtr_reg)
[add04f7]412 );
[897ad60]413}
414
415/** Load IDTR register from memory.
416 *
417 * @param idtr_reg Address of memory from where to load IDTR.
[add04f7]418 *
[897ad60]419 */
[7a0359b]420NO_TRACE static inline void idtr_load(ptr_16_32_t *idtr_reg)
[897ad60]421{
[add04f7]422 asm volatile (
423 "lidtl %[idtr_reg]\n"
424 :: [idtr_reg] "m" (*idtr_reg)
425 );
[897ad60]426}
427
428/** Load TR from descriptor table.
429 *
430 * @param sel Selector specifying descriptor of TSS segment.
[add04f7]431 *
[897ad60]432 */
[7a0359b]433NO_TRACE static inline void tr_load(uint16_t sel)
[897ad60]434{
[add04f7]435 asm volatile (
436 "ltr %[sel]"
437 :: [sel] "r" (sel)
438 );
[897ad60]439}
440
[7a0359b]441extern void paging_on(void);
442extern void enable_l_apic_in_msr(void);
443
444extern void asm_delay_loop(uint32_t);
445extern void asm_fake_loop(uint32_t);
446
[44c69b66]447extern uintptr_t int_syscall;
448
[b808660]449extern uintptr_t int_0;
450extern uintptr_t int_1;
451extern uintptr_t int_2;
452extern uintptr_t int_3;
453extern uintptr_t int_4;
454extern uintptr_t int_5;
455extern uintptr_t int_6;
456extern uintptr_t int_7;
457extern uintptr_t int_8;
458extern uintptr_t int_9;
459extern uintptr_t int_10;
460extern uintptr_t int_11;
461extern uintptr_t int_12;
462extern uintptr_t int_13;
463extern uintptr_t int_14;
464extern uintptr_t int_15;
465extern uintptr_t int_16;
466extern uintptr_t int_17;
467extern uintptr_t int_18;
468extern uintptr_t int_19;
469extern uintptr_t int_20;
470extern uintptr_t int_21;
471extern uintptr_t int_22;
472extern uintptr_t int_23;
473extern uintptr_t int_24;
474extern uintptr_t int_25;
475extern uintptr_t int_26;
476extern uintptr_t int_27;
477extern uintptr_t int_28;
478extern uintptr_t int_29;
479extern uintptr_t int_30;
480extern uintptr_t int_31;
481extern uintptr_t int_32;
482extern uintptr_t int_33;
483extern uintptr_t int_34;
484extern uintptr_t int_35;
485extern uintptr_t int_36;
486extern uintptr_t int_37;
487extern uintptr_t int_38;
488extern uintptr_t int_39;
489extern uintptr_t int_40;
490extern uintptr_t int_41;
491extern uintptr_t int_42;
492extern uintptr_t int_43;
493extern uintptr_t int_44;
494extern uintptr_t int_45;
495extern uintptr_t int_46;
496extern uintptr_t int_47;
497extern uintptr_t int_48;
498extern uintptr_t int_49;
499extern uintptr_t int_50;
500extern uintptr_t int_51;
501extern uintptr_t int_52;
502extern uintptr_t int_53;
503extern uintptr_t int_54;
504extern uintptr_t int_55;
505extern uintptr_t int_56;
506extern uintptr_t int_57;
507extern uintptr_t int_58;
508extern uintptr_t int_59;
509extern uintptr_t int_60;
510extern uintptr_t int_61;
511extern uintptr_t int_62;
512extern uintptr_t int_63;
513
[f761f1eb]514#endif
[b45c443]515
[06e1e95]516/** @}
[b45c443]517 */
Note: See TracBrowser for help on using the repository browser.