Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
173 jermar 1
/*
2
 * Copyright (C) 2005 Jakub Jermar
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
 
1702 cejka 29
 /** @addtogroup amd64 
30
 * @{
31
 */
32
/** @file
33
 */
34
 
173 jermar 35
#ifndef __amd64_ASM_H__
36
#define __amd64_ASM_H__
37
 
1186 jermar 38
#include <arch/pm.h>
173 jermar 39
#include <arch/types.h>
40
#include <config.h>
41
 
597 jermar 42
extern void asm_delay_loop(__u32 t);
43
extern void asm_fake_loop(__u32 t);
200 palkovsky 44
 
253 jermar 45
/** Return base address of current stack.
46
 *
47
 * Return the base address of the current stack.
48
 * The stack is assumed to be STACK_SIZE bytes long.
49
 * The stack must start on page boundary.
50
 */
173 jermar 51
static inline __address get_stack_base(void)
52
{
226 palkovsky 53
    __address v;
54
 
55
    __asm__ volatile ("andq %%rsp, %0\n" : "=r" (v) : "0" (~((__u64)STACK_SIZE-1)));
56
 
57
    return v;
173 jermar 58
}
59
 
348 jermar 60
static inline void cpu_sleep(void) { __asm__ volatile ("hlt\n"); };
61
static inline void cpu_halt(void) { __asm__ volatile ("hlt\n"); };
197 palkovsky 62
 
200 palkovsky 63
 
625 palkovsky 64
/** Byte from port
65
 *
66
 * Get byte from port
67
 *
68
 * @param port Port to read from
69
 * @return Value read
70
 */
71
static inline __u8 inb(__u16 port) { __u8 val; __asm__ volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) ); return val; }
200 palkovsky 72
 
625 palkovsky 73
/** Byte to port
74
 *
75
 * Output byte to port
76
 *
77
 * @param port Port to write to
78
 * @param val Value to write
79
 */
80
static inline void outb(__u16 port, __u8 val) { __asm__ volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) ); }
200 palkovsky 81
 
806 palkovsky 82
/** Swap Hidden part of GS register with visible one */
83
static inline void swapgs(void) { __asm__ volatile("swapgs"); }
84
 
413 jermar 85
/** Enable interrupts.
200 palkovsky 86
 *
87
 * Enable interrupts and return previous
88
 * value of EFLAGS.
413 jermar 89
 *
90
 * @return Old interrupt priority level.
200 palkovsky 91
 */
413 jermar 92
static inline ipl_t interrupts_enable(void) {
93
    ipl_t v;
200 palkovsky 94
    __asm__ volatile (
95
        "pushfq\n"
96
        "popq %0\n"
97
        "sti\n"
98
        : "=r" (v)
99
    );
100
    return v;
101
}
102
 
413 jermar 103
/** Disable interrupts.
200 palkovsky 104
 *
105
 * Disable interrupts and return previous
106
 * value of EFLAGS.
413 jermar 107
 *
108
 * @return Old interrupt priority level.
200 palkovsky 109
 */
413 jermar 110
static inline ipl_t interrupts_disable(void) {
111
    ipl_t v;
200 palkovsky 112
    __asm__ volatile (
113
        "pushfq\n"
114
        "popq %0\n"
115
        "cli\n"
116
        : "=r" (v)
117
        );
118
    return v;
119
}
120
 
413 jermar 121
/** Restore interrupt priority level.
200 palkovsky 122
 *
123
 * Restore EFLAGS.
413 jermar 124
 *
125
 * @param ipl Saved interrupt priority level.
200 palkovsky 126
 */
413 jermar 127
static inline void interrupts_restore(ipl_t ipl) {
200 palkovsky 128
    __asm__ volatile (
129
        "pushq %0\n"
130
        "popfq\n"
413 jermar 131
        : : "r" (ipl)
200 palkovsky 132
        );
133
}
134
 
413 jermar 135
/** Return interrupt priority level.
206 palkovsky 136
 *
137
 * Return EFLAFS.
413 jermar 138
 *
139
 * @return Current interrupt priority level.
206 palkovsky 140
 */
413 jermar 141
static inline ipl_t interrupts_read(void) {
142
    ipl_t v;
206 palkovsky 143
    __asm__ volatile (
144
        "pushfq\n"
145
        "popq %0\n"
146
        : "=r" (v)
147
    );
148
    return v;
149
}
200 palkovsky 150
 
803 palkovsky 151
/** Write to MSR */
152
static inline void write_msr(__u32 msr, __u64 value)
153
{
154
    __asm__ volatile (
155
        "wrmsr;" : : "c" (msr),
156
        "a" ((__u32)(value)),
157
        "d" ((__u32)(value >> 32))
158
        );
159
}
219 palkovsky 160
 
803 palkovsky 161
static inline __native read_msr(__u32 msr)
162
{
163
    __u32 ax, dx;
164
 
165
    __asm__ volatile (
166
        "rdmsr;" : "=a"(ax), "=d"(dx) : "c" (msr)
167
        );
168
    return ((__u64)dx << 32) | ax;
169
}
170
 
171
 
268 palkovsky 172
/** Enable local APIC
173
 *
174
 * Enable local APIC in MSR.
175
 */
176
static inline void enable_l_apic_in_msr()
177
{
178
    __asm__ volatile (
348 jermar 179
        "movl $0x1b, %%ecx\n"
180
        "rdmsr\n"
181
        "orl $(1<<11),%%eax\n"
182
        "orl $(0xfee00000),%%eax\n"
183
        "wrmsr\n"
268 palkovsky 184
        :
185
        :
186
        :"%eax","%ecx","%edx"
187
        );
188
}
189
 
581 palkovsky 190
static inline __address * get_ip()
191
{
192
    __address *ip;
193
 
194
    __asm__ volatile (
195
        "mov %%rip, %0"
196
        : "=r" (ip)
197
        );
198
    return ip;
199
}
200
 
597 jermar 201
/** Invalidate TLB Entry.
202
 *
203
 * @param addr Address on a page whose TLB entry is to be invalidated.
204
 */
205
static inline void invlpg(__address addr)
206
{
1186 jermar 207
    __asm__ volatile ("invlpg %0\n" :: "m" (*((__native *)addr)));
597 jermar 208
}
581 palkovsky 209
 
1186 jermar 210
/** Load GDTR register from memory.
211
 *
212
 * @param gdtr_reg Address of memory from where to load GDTR.
213
 */
214
static inline void gdtr_load(struct ptr_16_64 *gdtr_reg)
215
{
1251 jermar 216
    __asm__ volatile ("lgdtq %0\n" : : "m" (*gdtr_reg));
1186 jermar 217
}
218
 
219
/** Store GDTR register to memory.
220
 *
221
 * @param gdtr_reg Address of memory to where to load GDTR.
222
 */
223
static inline void gdtr_store(struct ptr_16_64 *gdtr_reg)
224
{
1251 jermar 225
    __asm__ volatile ("sgdtq %0\n" : : "m" (*gdtr_reg));
1186 jermar 226
}
227
 
228
/** Load IDTR register from memory.
229
 *
230
 * @param idtr_reg Address of memory from where to load IDTR.
231
 */
232
static inline void idtr_load(struct ptr_16_64 *idtr_reg)
233
{
1251 jermar 234
    __asm__ volatile ("lidtq %0\n" : : "m" (*idtr_reg));
1186 jermar 235
}
236
 
237
/** Load TR from descriptor table.
238
 *
239
 * @param sel Selector specifying descriptor of TSS segment.
240
 */
241
static inline void tr_load(__u16 sel)
242
{
243
    __asm__ volatile ("ltr %0" : : "r" (sel));
244
}
245
 
1072 palkovsky 246
#define GEN_READ_REG(reg) static inline __native read_ ##reg (void) \
247
    { \
248
    __native res; \
249
    __asm__ volatile ("movq %%" #reg ", %0" : "=r" (res) ); \
250
    return res; \
251
    }
252
 
253
#define GEN_WRITE_REG(reg) static inline void write_ ##reg (__native regn) \
254
    { \
255
    __asm__ volatile ("movq %0, %%" #reg : : "r" (regn)); \
256
    }
257
 
258
GEN_READ_REG(cr0);
259
GEN_READ_REG(cr2);
260
GEN_READ_REG(cr3);
261
GEN_WRITE_REG(cr3);
262
 
263
GEN_READ_REG(dr0);
264
GEN_READ_REG(dr1);
265
GEN_READ_REG(dr2);
266
GEN_READ_REG(dr3);
267
GEN_READ_REG(dr6);
268
GEN_READ_REG(dr7);
269
 
270
GEN_WRITE_REG(dr0);
271
GEN_WRITE_REG(dr1);
272
GEN_WRITE_REG(dr2);
273
GEN_WRITE_REG(dr3);
274
GEN_WRITE_REG(dr6);
275
GEN_WRITE_REG(dr7);
276
 
277
 
206 palkovsky 278
extern size_t interrupt_handler_size;
279
extern void interrupt_handlers(void);
280
 
173 jermar 281
#endif
1702 cejka 282
 
283
 /** @}
284
 */
285