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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since abf04a54 was abf04a54, checked in by Jakub Jermar <jakub@…>, 14 years ago

Merge PIO improvement from Zdenek Bouska.

  • Property mode set to 100644
File size: 9.8 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
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
30/** @addtogroup ia32
31 * @{
32 */
33/** @file
34 */
35
36#ifndef KERN_ia32_ASM_H_
37#define KERN_ia32_ASM_H_
38
39#include <arch/pm.h>
40#include <arch/cpu.h>
41#include <typedefs.h>
42#include <config.h>
43#include <trace.h>
44
45/** Halt CPU
46 *
47 * Halt the current CPU.
48 *
49 */
50NO_TRACE static inline __attribute__((noreturn)) void cpu_halt(void)
51{
52 while (true) {
53 asm volatile (
54 "hlt\n"
55 );
56 }
57}
58
59NO_TRACE static inline void cpu_sleep(void)
60{
61 asm volatile (
62 "hlt\n"
63 );
64}
65
66#define GEN_READ_REG(reg) NO_TRACE static inline sysarg_t read_ ##reg (void) \
67 { \
68 sysarg_t res; \
69 asm volatile ( \
70 "movl %%" #reg ", %[res]" \
71 : [res] "=r" (res) \
72 ); \
73 return res; \
74 }
75
76#define GEN_WRITE_REG(reg) NO_TRACE static inline void write_ ##reg (sysarg_t regn) \
77 { \
78 asm volatile ( \
79 "movl %[regn], %%" #reg \
80 :: [regn] "r" (regn) \
81 ); \
82 }
83
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)
102
103#define IO_SPACE_BOUNDARY ((void *) (64 * 1024))
104
105/** Byte to port
106 *
107 * Output byte to port
108 *
109 * @param port Port to write to
110 * @param val Value to write
111 *
112 */
113NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t val)
114{
115 if (port < (ioport8_t *) IO_SPACE_BOUNDARY) {
116 asm volatile (
117 "outb %b[val], %w[port]\n"
118 :: [val] "a" (val), [port] "d" (port)
119 );
120 } else {
121 *port = val;
122 }
123}
124
125/** Word to port
126 *
127 * Output word to port
128 *
129 * @param port Port to write to
130 * @param val Value to write
131 *
132 */
133NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t val)
134{
135 if (port < (ioport16_t *) IO_SPACE_BOUNDARY) {
136 asm volatile (
137 "outw %w[val], %w[port]\n"
138 :: [val] "a" (val), [port] "d" (port)
139 );
140 } else {
141 *port = val;
142 }
143}
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
151 *
152 */
153NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t val)
154{
155 if (port < (ioport32_t *) IO_SPACE_BOUNDARY) {
156 asm volatile (
157 "outl %[val], %w[port]\n"
158 :: [val] "a" (val), [port] "d" (port)
159 );
160 } else {
161 *port = val;
162 }
163}
164
165/** Byte from port
166 *
167 * Get byte from port
168 *
169 * @param port Port to read from
170 * @return Value read
171 *
172 */
173NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
174{
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 }
186}
187
188/** Word from port
189 *
190 * Get word from port
191 *
192 * @param port Port to read from
193 * @return Value read
194 *
195 */
196NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
197{
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 }
209}
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
217 *
218 */
219NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
220{
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 }
232}
233
234/** Enable interrupts.
235 *
236 * Enable interrupts and return previous
237 * value of EFLAGS.
238 *
239 * @return Old interrupt priority level.
240 *
241 */
242NO_TRACE static inline ipl_t interrupts_enable(void)
243{
244 ipl_t v;
245
246 asm volatile (
247 "pushf\n"
248 "popl %[v]\n"
249 "sti\n"
250 : [v] "=r" (v)
251 );
252
253 return v;
254}
255
256/** Disable interrupts.
257 *
258 * Disable interrupts and return previous
259 * value of EFLAGS.
260 *
261 * @return Old interrupt priority level.
262 *
263 */
264NO_TRACE static inline ipl_t interrupts_disable(void)
265{
266 ipl_t v;
267
268 asm volatile (
269 "pushf\n"
270 "popl %[v]\n"
271 "cli\n"
272 : [v] "=r" (v)
273 );
274
275 return v;
276}
277
278/** Restore interrupt priority level.
279 *
280 * Restore EFLAGS.
281 *
282 * @param ipl Saved interrupt priority level.
283 *
284 */
285NO_TRACE static inline void interrupts_restore(ipl_t ipl)
286{
287 asm volatile (
288 "pushl %[ipl]\n"
289 "popf\n"
290 :: [ipl] "r" (ipl)
291 );
292}
293
294/** Return interrupt priority level.
295 *
296 * @return EFLAFS.
297 *
298 */
299NO_TRACE static inline ipl_t interrupts_read(void)
300{
301 ipl_t v;
302
303 asm volatile (
304 "pushf\n"
305 "popl %[v]\n"
306 : [v] "=r" (v)
307 );
308
309 return v;
310}
311
312/** Check interrupts state.
313 *
314 * @return True if interrupts are disabled.
315 *
316 */
317NO_TRACE static inline bool interrupts_disabled(void)
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
330#ifndef PROCESSOR_i486
331
332/** Write to MSR */
333NO_TRACE static inline void write_msr(uint32_t msr, uint64_t value)
334{
335 asm volatile (
336 "wrmsr"
337 :: "c" (msr),
338 "a" ((uint32_t) (value)),
339 "d" ((uint32_t) (value >> 32))
340 );
341}
342
343NO_TRACE static inline uint64_t read_msr(uint32_t msr)
344{
345 uint32_t ax, dx;
346
347 asm volatile (
348 "rdmsr"
349 : "=a" (ax),
350 "=d" (dx)
351 : "c" (msr)
352 );
353
354 return ((uint64_t) dx << 32) | ax;
355}
356
357#endif /* PROCESSOR_i486 */
358
359
360/** Return base address of current stack
361 *
362 * Return the base address of the current stack.
363 * The stack is assumed to be STACK_SIZE bytes long.
364 * The stack must start on page boundary.
365 *
366 */
367NO_TRACE static inline uintptr_t get_stack_base(void)
368{
369 uintptr_t v;
370
371 asm volatile (
372 "andl %%esp, %[v]\n"
373 : [v] "=r" (v)
374 : "0" (~(STACK_SIZE - 1))
375 );
376
377 return v;
378}
379
380/** Invalidate TLB Entry.
381 *
382 * @param addr Address on a page whose TLB entry is to be invalidated.
383 *
384 */
385NO_TRACE static inline void invlpg(uintptr_t addr)
386{
387 asm volatile (
388 "invlpg %[addr]\n"
389 :: [addr] "m" (*(sysarg_t *) addr)
390 );
391}
392
393/** Load GDTR register from memory.
394 *
395 * @param gdtr_reg Address of memory from where to load GDTR.
396 *
397 */
398NO_TRACE static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
399{
400 asm volatile (
401 "lgdtl %[gdtr_reg]\n"
402 :: [gdtr_reg] "m" (*gdtr_reg)
403 );
404}
405
406/** Store GDTR register to memory.
407 *
408 * @param gdtr_reg Address of memory to where to load GDTR.
409 *
410 */
411NO_TRACE static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
412{
413 asm volatile (
414 "sgdtl %[gdtr_reg]\n"
415 : [gdtr_reg] "=m" (*gdtr_reg)
416 );
417}
418
419/** Load IDTR register from memory.
420 *
421 * @param idtr_reg Address of memory from where to load IDTR.
422 *
423 */
424NO_TRACE static inline void idtr_load(ptr_16_32_t *idtr_reg)
425{
426 asm volatile (
427 "lidtl %[idtr_reg]\n"
428 :: [idtr_reg] "m" (*idtr_reg)
429 );
430}
431
432/** Load TR from descriptor table.
433 *
434 * @param sel Selector specifying descriptor of TSS segment.
435 *
436 */
437NO_TRACE static inline void tr_load(uint16_t sel)
438{
439 asm volatile (
440 "ltr %[sel]"
441 :: [sel] "r" (sel)
442 );
443}
444
445extern void paging_on(void);
446extern void enable_l_apic_in_msr(void);
447
448extern void asm_delay_loop(uint32_t);
449extern void asm_fake_loop(uint32_t);
450
451extern uintptr_t int_syscall;
452
453extern uintptr_t int_0;
454extern uintptr_t int_1;
455extern uintptr_t int_2;
456extern uintptr_t int_3;
457extern uintptr_t int_4;
458extern uintptr_t int_5;
459extern uintptr_t int_6;
460extern uintptr_t int_7;
461extern uintptr_t int_8;
462extern uintptr_t int_9;
463extern uintptr_t int_10;
464extern uintptr_t int_11;
465extern uintptr_t int_12;
466extern uintptr_t int_13;
467extern uintptr_t int_14;
468extern uintptr_t int_15;
469extern uintptr_t int_16;
470extern uintptr_t int_17;
471extern uintptr_t int_18;
472extern uintptr_t int_19;
473extern uintptr_t int_20;
474extern uintptr_t int_21;
475extern uintptr_t int_22;
476extern uintptr_t int_23;
477extern uintptr_t int_24;
478extern uintptr_t int_25;
479extern uintptr_t int_26;
480extern uintptr_t int_27;
481extern uintptr_t int_28;
482extern uintptr_t int_29;
483extern uintptr_t int_30;
484extern uintptr_t int_31;
485extern uintptr_t int_32;
486extern uintptr_t int_33;
487extern uintptr_t int_34;
488extern uintptr_t int_35;
489extern uintptr_t int_36;
490extern uintptr_t int_37;
491extern uintptr_t int_38;
492extern uintptr_t int_39;
493extern uintptr_t int_40;
494extern uintptr_t int_41;
495extern uintptr_t int_42;
496extern uintptr_t int_43;
497extern uintptr_t int_44;
498extern uintptr_t int_45;
499extern uintptr_t int_46;
500extern uintptr_t int_47;
501extern uintptr_t int_48;
502extern uintptr_t int_49;
503extern uintptr_t int_50;
504extern uintptr_t int_51;
505extern uintptr_t int_52;
506extern uintptr_t int_53;
507extern uintptr_t int_54;
508extern uintptr_t int_55;
509extern uintptr_t int_56;
510extern uintptr_t int_57;
511extern uintptr_t int_58;
512extern uintptr_t int_59;
513extern uintptr_t int_60;
514extern uintptr_t int_61;
515extern uintptr_t int_62;
516extern uintptr_t int_63;
517
518#endif
519
520/** @}
521 */
Note: See TracBrowser for help on using the repository browser.