Project

General

Profile

Statistics
| Branch: | Revision:

root / prex-0.9.0 / bsp / hal / x86 / include / cpu.h @ 03e9c04a

History | View | Annotate | Download (5.71 KB)

1
/*-
2
 * Copyright (c) 2005, Kohsuke Ohtani
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
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the author nor the names of any co-contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
#ifndef _X86_CPU_H
31
#define _X86_CPU_H
32

    
33
#include <sys/cdefs.h>        /* for __packed */
34

    
35
/*
36
 * GDTs
37
 */
38
#define KERNEL_CS        0x10
39
#define KERNEL_DS        0x18
40
#define USER_CS                0x20
41
#define USER_DS                0x28
42
#define KERNEL_TSS        0x38
43

    
44
#define NGDTS                8
45

    
46
/*
47
 * IDTs
48
 */
49
#define NIDTS                0x41
50
#define SYSCALL_INT        0x40
51
#define INVALID_INT        0xFF
52

    
53
/*
54
 * x86 flags register
55
 */
56
#define EFL_CF                0x00000001        /* Carry */
57
#define EFL_PF                0x00000004        /* Parity */
58
#define EFL_AF                0x00000010        /* Carry */
59
#define EFL_ZF                0x00000040        /* Zero */
60
#define EFL_SF                0x00000080        /* Sign */
61
#define EFL_TF                0x00000100        /* Trap */
62
#define EFL_IF                0x00000200        /* Interrupt enable */
63
#define EFL_DF                0x00000400        /* Direction */
64
#define EFL_OF                0x00000800        /* Overflow */
65
#define EFL_IOPL        0x00003000        /* IO privilege level: */
66
#define EFL_IOPL_KERN        0x00000000        /* Kernel */
67
#define EFL_IOPL_USER        0x00003000        /* User */
68
#define EFL_NT                0x00004000        /* Nested task */
69
#define EFL_RF                0x00010000        /* Resume without tracing */
70
#define EFL_VM                0x00020000        /* Virtual 8086 mode */
71
#define EFL_AC                0x00040000        /* Alignment Check */
72

    
73
/*
74
 * CR0 register
75
 */
76
#define CR0_PG                0x80000000        /* enable paging */
77
#define CR0_CD                0x40000000        /* cache disable */
78
#define CR0_NW                0x20000000        /* no write-through */
79
#define CR0_AM                0x00040000        /* alignment check mask */
80
#define CR0_WP                0x00010000        /* write-protect kernel access */
81
#define CR0_NE                0x00000020        /* handle numeric exceptions */
82
#define CR0_ET                0x00000010        /* extension type is 80387 coprocessor */
83
#define CR0_TS                0x00000008        /* task switch */
84
#define CR0_EM                0x00000004        /* emulate coprocessor */
85
#define CR0_MP                0x00000002        /* monitor coprocessor */
86
#define CR0_PE                0x00000001        /* enable protected mode */
87

    
88
#ifndef __ASSEMBLY__
89

    
90
#include <sys/types.h>
91
#include <context.h>
92

    
93
#if defined(__SUNPRO_C)
94
#pragma pack(1)
95
#endif
96

    
97
/*
98
 * Segment Descriptor
99
 */
100
struct seg_desc {
101
        u_int limit_lo:16;        /* segment limit (lsb) */
102
        u_int base_lo:16;        /* segment base address (lsb) */
103
        u_int base_mid:8;        /* segment base address (middle) */
104
        u_int type:8;                /* type */
105
        u_int limit_hi:4;        /* segment limit (msb) */
106
        u_int size:4;                /* size */
107
        u_int base_hi:8;        /* segment base address (msb) */
108
} __packed;
109

    
110
/*
111
 * Gate Descriptor
112
 */
113
struct gate_desc {
114
        u_int offset_lo:16;        /* gate offset (lsb) */
115
        u_int selector:16;        /* gate segment selector */
116
        u_int nr_copy:8;        /* stack copy count */
117
        u_int type:8;                /* type */
118
        u_int offset_hi:16;        /* gate offset (msb) */
119
} __packed;
120

    
121
/*
122
 * Linear memory description for lgdt and lidt instructions.
123
 */
124
struct desc_p {
125
        uint16_t limit;
126
        uint32_t base;
127
} __packed;
128

    
129
/*
130
 * Segment size
131
 */
132
#define SIZE_32                0x4        /* 32-bit segment */
133
#define SIZE_16                0x0        /* 16-bit segment */
134
#define SIZE_4K                0x8        /* 4K limit field */
135

    
136
/*
137
 * Segment type
138
 */
139
#define ST_ACC                0x01        /* accessed */
140
#define ST_LDT                0x02        /* LDT */
141
#define ST_CALL_GATE_16        0x04        /* 16-bit call gate */
142
#define ST_TASK_GATE        0x05        /* task gate */
143
#define ST_TSS                0x09        /* task segment */
144
#define ST_CALL_GATE        0x0c        /* call gate */
145
#define ST_INTR_GATE        0x0e        /* interrupt gate */
146
#define ST_TRAP_GATE        0x0f        /* trap gate */
147

    
148
#define ST_TSS_BUSY        0x02        /* task busy */
149

    
150
#define ST_DATA                0x10        /* data */
151
#define ST_DATA_W        0x12        /* data, writable */
152
#define ST_DATA_E        0x14        /* data, expand-down */
153
#define ST_DATA_EW        0x16        /* data, expand-down, writable */
154

    
155
#define ST_CODE                0x18        /* code */
156
#define ST_CODE_R        0x1a        /* code, readable */
157
#define ST_CODE_C        0x1c        /* code, conforming */
158
#define ST_CODE_CR        0x1e        /* code, conforming, readable */
159

    
160
#define ST_KERN                0x00        /* kernel access only */
161
#define ST_USER                0x60        /* user access */
162

    
163
#define ST_PRESENT        0x80        /* segment present */
164

    
165
/*
166
 * Task State Segment (TSS)
167
 */
168

    
169
#define IO_BITMAP_SIZE                (65536/8 + 1)
170
#define INVALID_IO_BITMAP        0x8000
171

    
172
struct tss {
173
        uint32_t back_link;
174
        uint32_t esp0, ss0;
175
        uint32_t esp1, ss1;
176
        uint32_t esp2, ss2;
177
        uint32_t cr3;
178
        uint32_t eip;
179
        uint32_t eflags;
180
        uint32_t eax, ecx, edx, ebx;
181
        uint32_t esp, ebp, esi, edi;
182
        uint32_t es, cs, ss, ds, fs, gs;
183
        uint32_t ldt;
184
        uint16_t dbg_trace;
185
        uint16_t io_bitmap_offset;
186
#if 0
187
        uint32_t io_bitmap[IO_BITMAP_SIZE/4+1];
188
        uint32_t pad[5];
189
#endif
190
} __packed;
191

    
192

    
193
#if defined(__SUNPRO_C)
194
#pragma pack()
195
#endif
196

    
197
__BEGIN_DECLS
198
void         tss_set(uint32_t);
199
uint32_t tss_get(void);
200
void         cpu_init(void);
201
__END_DECLS
202

    
203
#endif /* !__ASSEMBLY__ */
204
#endif /* !_X86_CPU_H */