Subversion Repositories HelenOS-historic

Rev

Rev 363 | Rev 413 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
393 bondari 3
 * Copyright (C) 2005 Sergey Bondari
1 jermar 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
 
11 jermar 30
#ifndef __ia32_ASM_H__
31
#define __ia32_ASM_H__
1 jermar 32
 
33
#include <arch/types.h>
177 jermar 34
#include <config.h>
1 jermar 35
 
36
extern __u32 interrupt_handler_size;
37
 
38
extern void paging_on(void);
39
 
40
extern void interrupt_handlers(void);
41
 
42
extern void enable_l_apic_in_msr(void);
43
 
195 vana 44
 
45
void asm_delay_loop(__u32 t);
46
void asm_fake_loop(__u32 t);
47
 
48
 
115 jermar 49
/** Halt CPU
50
 *
51
 * Halt the current CPU until interrupt event.
52
 */
348 jermar 53
static inline void cpu_halt(void) { __asm__("hlt\n"); };
54
static inline void cpu_sleep(void) { __asm__("hlt\n"); };
1 jermar 55
 
115 jermar 56
/** Read CR2
57
 *
58
 * Return value in CR2
59
 *
60
 * @return Value read.
61
 */
348 jermar 62
static inline __u32 read_cr2(void) { __u32 v; __asm__ volatile ("movl %%cr2,%0\n" : "=r" (v)); return v; }
27 jermar 63
 
115 jermar 64
/** Write CR3
65
 *
66
 * Write value to CR3.
67
 *
68
 * @param v Value to be written.
69
 */
70
static inline void write_cr3(__u32 v) { __asm__ volatile ("movl %0,%%cr3\n" : : "r" (v)); }
38 jermar 71
 
115 jermar 72
/** Read CR3
73
 *
74
 * Return value in CR3
75
 *
76
 * @return Value read.
77
 */
348 jermar 78
static inline __u32 read_cr3(void) { __u32 v; __asm__ volatile ("movl %%cr3,%0\n" : "=r" (v)); return v; }
115 jermar 79
 
352 bondari 80
/** Byte to port
81
 *
82
 * Output byte to port
83
 *
84
 * @param port Port to write to
85
 * @param val Value to write
86
 */
87
static inline void outb(__u16 port, __u8 val) { __asm__ volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) ); }
88
 
353 bondari 89
/** Word to port
90
 *
91
 * Output word to port
92
 *
93
 * @param port Port to write to
94
 * @param val Value to write
95
 */
96
static inline void outw(__u16 port, __u16 val) { __asm__ volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port) ); }
352 bondari 97
 
353 bondari 98
/** Double word to port
99
 *
100
 * Output double word to port
101
 *
102
 * @param port Port to write to
103
 * @param val Value to write
104
 */
105
static inline void outl(__u16 port, __u32 val) { __asm__ volatile ("outl %l0, %w1\n" : : "a" (val), "d" (port) ); }
106
 
356 bondari 107
/** Byte from port
108
 *
109
 * Get byte from port
110
 *
111
 * @param port Port to read from
112
 * @return Value read
113
 */
114
static inline __u8 inb(__u16 port) { __u8 val; __asm__ volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) ); return val; }
115
 
116
/** Word from port
117
 *
118
 * Get word from port
119
 *
120
 * @param port Port to read from
121
 * @return Value read
122
 */
123
static inline __u16 inw(__u16 port) { __u16 val; __asm__ volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port) ); return val; }
124
 
125
/** Double word from port
126
 *
127
 * Get double word from port
128
 *
129
 * @param port Port to read from
130
 * @return Value read
131
 */
132
static inline __u32 inl(__u16 port) { __u32 val; __asm__ volatile ("inl %w1, %l0 \n" : "=a" (val) : "d" (port) ); return val; }
133
 
115 jermar 134
/** Set priority level low
135
 *
136
 * Enable interrupts and return previous
137
 * value of EFLAGS.
138
 */
139
static inline pri_t cpu_priority_low(void) {
140
    pri_t v;
141
    __asm__ volatile (
358 bondari 142
        "pushf\n\t"
143
        "popl %0\n\t"
115 jermar 144
        "sti\n"
145
        : "=r" (v)
146
    );
147
    return v;
148
}
149
 
150
/** Set priority level high
151
 *
152
 * Disable interrupts and return previous
153
 * value of EFLAGS.
154
 */
155
static inline pri_t cpu_priority_high(void) {
156
    pri_t v;
157
    __asm__ volatile (
358 bondari 158
        "pushf\n\t"
159
        "popl %0\n\t"
115 jermar 160
        "cli\n"
161
        : "=r" (v)
162
    );
163
    return v;
164
}
165
 
166
/** Restore priority level
167
 *
168
 * Restore EFLAGS.
169
 */
170
static inline void cpu_priority_restore(pri_t pri) {
171
    __asm__ volatile (
358 bondari 172
        "pushl %0\n\t"
115 jermar 173
        "popf\n"
174
        : : "r" (pri)
175
    );
176
}
177
 
178
/** Return raw priority level
179
 *
180
 * Return EFLAFS.
181
 */
182
static inline pri_t cpu_priority_read(void) {
183
    pri_t v;
184
    __asm__ volatile (
358 bondari 185
        "pushf\n\t"
115 jermar 186
        "popl %0\n"
187
        : "=r" (v)
188
    );
189
    return v;
190
}
191
 
173 jermar 192
/** Return base address of current stack
193
 *
194
 * Return the base address of the current stack.
195
 * The stack is assumed to be STACK_SIZE bytes long.
180 jermar 196
 * The stack must start on page boundary.
173 jermar 197
 */
198
static inline __address get_stack_base(void)
199
{
200
    __address v;
201
 
202
    __asm__ volatile ("andl %%esp, %0\n" : "=r" (v) : "0" (~(STACK_SIZE-1)));
203
 
204
    return v;
205
}
206
 
348 jermar 207
static inline __u64 rdtsc(void)
208
{
209
    __u64 v;
210
 
211
    __asm__ volatile("rdtsc\n" : "=A" (v));
212
 
213
    return v;
214
}
215
 
1 jermar 216
#endif