Annotation of prex/sys/arch/i386/include/cpu.h, Revision 1.1
1.1 ! nbrk 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 _I386_CPU_H
! 31: #define _I386_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: #ifndef __ASSEMBLY__
! 55:
! 56: /*
! 57: * Segment Descriptor
! 58: */
! 59: struct seg_desc {
! 60: u_int limit_lo:16; /* segment limit (lsb) */
! 61: u_int base_lo:16; /* segment base address (lsb) */
! 62: u_int base_mid:8; /* segment base address (middle) */
! 63: u_int type:8; /* type */
! 64: u_int limit_hi:4; /* segment limit (msb) */
! 65: u_int size:4; /* size */
! 66: u_int base_hi:8; /* segment base address (msb) */
! 67: } __packed;
! 68:
! 69: /*
! 70: * Gate Descriptor
! 71: */
! 72: struct gate_desc {
! 73: u_int offset_lo:16; /* gate offset (lsb) */
! 74: u_int selector:16; /* gate segment selector */
! 75: u_int nr_copy:8; /* stack copy count */
! 76: u_int type:8; /* type */
! 77: u_int offset_hi:16; /* gate offset (msb) */
! 78: } __packed;
! 79:
! 80: /*
! 81: * Linear memory description for lgdt and lidt instructions.
! 82: */
! 83: struct desc_p {
! 84: uint16_t limit;
! 85: uint32_t base;
! 86: } __packed;
! 87:
! 88: /*
! 89: * Segment size
! 90: */
! 91: #define SIZE_32 0x4 /* 32-bit segment */
! 92: #define SIZE_16 0x0 /* 16-bit segment */
! 93: #define SIZE_4K 0x8 /* 4K limit field */
! 94:
! 95: /*
! 96: * Segment type
! 97: */
! 98: #define ST_ACC 0x01 /* accessed */
! 99: #define ST_LDT 0x02 /* LDT */
! 100: #define ST_CALL_GATE_16 0x04 /* 16-bit call gate */
! 101: #define ST_TASK_GATE 0x05 /* task gate */
! 102: #define ST_TSS 0x09 /* task segment */
! 103: #define ST_CALL_GATE 0x0c /* call gate */
! 104: #define ST_INTR_GATE 0x0e /* interrupt gate */
! 105: #define ST_TRAP_GATE 0x0f /* trap gate */
! 106:
! 107: #define ST_TSS_BUSY 0x02 /* task busy */
! 108:
! 109: #define ST_DATA 0x10 /* data */
! 110: #define ST_DATA_W 0x12 /* data, writable */
! 111: #define ST_DATA_E 0x14 /* data, expand-down */
! 112: #define ST_DATA_EW 0x16 /* data, expand-down, writable */
! 113:
! 114: #define ST_CODE 0x18 /* code */
! 115: #define ST_CODE_R 0x1a /* code, readable */
! 116: #define ST_CODE_C 0x1c /* code, conforming */
! 117: #define ST_CODE_CR 0x1e /* code, conforming, readable */
! 118:
! 119: #define ST_KERN 0x00 /* kernel access only */
! 120: #define ST_USER 0x60 /* user access */
! 121:
! 122: #define ST_PRESENT 0x80 /* segment present */
! 123:
! 124: /*
! 125: * Task State Segment (TSS)
! 126: */
! 127:
! 128: #define IO_BITMAP_SIZE (65536/8 + 1)
! 129: #define INVALID_IO_BITMAP 0x8000
! 130:
! 131: struct tss {
! 132: uint32_t back_link;
! 133: uint32_t esp0, ss0;
! 134: uint32_t esp1, ss1;
! 135: uint32_t esp2, ss2;
! 136: uint32_t cr3;
! 137: uint32_t eip;
! 138: uint32_t eflags;
! 139: uint32_t eax, ecx, edx, ebx;
! 140: uint32_t esp, ebp, esi, edi;
! 141: uint32_t es, cs, ss, ds, fs, gs;
! 142: uint32_t ldt;
! 143: uint16_t dbg_trace;
! 144: uint16_t io_bitmap_offset;
! 145: #if 0
! 146: uint32_t io_bitmap[IO_BITMAP_SIZE/4+1];
! 147: uint32_t pad[5];
! 148: #endif
! 149: } __packed;
! 150:
! 151: /*
! 152: * i386 flags register
! 153: */
! 154: #define EFL_CF 0x00000001 /* Carry */
! 155: #define EFL_PF 0x00000004 /* Parity */
! 156: #define EFL_AF 0x00000010 /* Carry */
! 157: #define EFL_ZF 0x00000040 /* Zero */
! 158: #define EFL_SF 0x00000080 /* Sign */
! 159: #define EFL_TF 0x00000100 /* Trap */
! 160: #define EFL_IF 0x00000200 /* Interrupt enable */
! 161: #define EFL_DF 0x00000400 /* Direction */
! 162: #define EFL_OF 0x00000800 /* Overflow */
! 163: #define EFL_IOPL 0x00003000 /* IO privilege level: */
! 164: #define EFL_IOPL_KERN 0x00000000 /* Kernel */
! 165: #define EFL_IOPL_USER 0x00003000 /* User */
! 166: #define EFL_NT 0x00004000 /* Nested task */
! 167: #define EFL_RF 0x00010000 /* Resume without tracing */
! 168: #define EFL_VM 0x00020000 /* Virtual 8086 mode */
! 169: #define EFL_AC 0x00040000 /* Alignment Check */
! 170:
! 171: /*
! 172: * CR0 register
! 173: */
! 174: #define CR0_PG 0x80000000 /* enable paging */
! 175: #define CR0_CD 0x40000000 /* cache disable */
! 176: #define CR0_NW 0x20000000 /* no write-through */
! 177: #define CR0_AM 0x00040000 /* alignment check mask */
! 178: #define CR0_WP 0x00010000 /* write-protect kernel access */
! 179: #define CR0_NE 0x00000020 /* handle numeric exceptions */
! 180: #define CR0_ET 0x00000010 /* extension type is 80387 coprocessor */
! 181: #define CR0_TS 0x00000008 /* task switch */
! 182: #define CR0_EM 0x00000004 /* emulate coprocessor */
! 183: #define CR0_MP 0x00000002 /* monitor coprocessor */
! 184: #define CR0_PE 0x00000001 /* enable protected mode */
! 185:
! 186: /*
! 187: * Page table (PTE)
! 188: */
! 189: typedef uint32_t *page_table_t;
! 190:
! 191: /*
! 192: * Page directory entry
! 193: */
! 194: #define PDE_PRESENT 0x00000001
! 195: #define PDE_WRITE 0x00000002
! 196: #define PDE_USER 0x00000004
! 197: #define PDE_WTHRU 0x00000008
! 198: #define PDE_NCACHE 0x00000010
! 199: #define PDE_ACCESS 0x00000020
! 200: #define PDE_SIZE 0x00000080
! 201: #define PDE_AVAIL 0x00000e00
! 202: #define PDE_ADDRESS 0xfffff000
! 203:
! 204: /*
! 205: * Page table entry
! 206: */
! 207: #define PTE_PRESENT 0x00000001
! 208: #define PTE_WRITE 0x00000002
! 209: #define PTE_USER 0x00000004
! 210: #define PTE_WTHRU 0x00000008
! 211: #define PTE_NCACHE 0x00000010
! 212: #define PTE_ACCESS 0x00000020
! 213: #define PTE_DIRTY 0x00000040
! 214: #define PTE_AVAIL 0x00000e00
! 215: #define PTE_ADDRESS 0xfffff000
! 216:
! 217: /*
! 218: * Virtual and physical address translation
! 219: */
! 220: #define PAGE_DIR(virt) (int)((((vaddr_t)(virt)) >> 22) & 0x3ff)
! 221: #define PAGE_TABLE(virt) (int)((((vaddr_t)(virt)) >> 12) & 0x3ff)
! 222:
! 223: #define pte_present(pgd, virt) (pgd[PAGE_DIR(virt)] & PDE_PRESENT)
! 224:
! 225: #define page_present(pte, virt) (pte[PAGE_TABLE(virt)] & PTE_PRESENT)
! 226:
! 227: #define pgd_to_pte(pgd, virt) \
! 228: (page_table_t)phys_to_virt((pgd)[PAGE_DIR(virt)] & PDE_ADDRESS)
! 229:
! 230: #define pte_to_page(pte, virt) \
! 231: ((pte)[PAGE_TABLE(virt)] & PTE_ADDRESS)
! 232:
! 233: extern void tss_set(uint32_t kstack);
! 234: extern uint32_t tss_get(void);
! 235: extern void trap_set(int vector, void (*handler)(void));
! 236: extern void trap_handler(struct cpu_regs *);
! 237: extern void cpu_init(void);
! 238:
! 239: #endif /* !__ASSEMBLY__ */
! 240:
! 241: #endif /* !_I386_CPU_H */
CVSweb