[1] | 1 | /* |
---|
| 2 | * cpu-internal.c - cpu internal data structures |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 6 | * |
---|
| 7 | * This file is part of ALMOS-kernel. |
---|
| 8 | * |
---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 10 | * under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 12 | * |
---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <config.h> |
---|
| 24 | #include <types.h> |
---|
| 25 | #include <cpu.h> |
---|
| 26 | #include <cpu-internal.h> |
---|
| 27 | #include <libk.h> |
---|
| 28 | #include <hardware.h> |
---|
| 29 | #include <pic.h> |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | #define GDT_ENTRIES_NR GDT_FIXED_NR + CPU_NR |
---|
| 33 | #define IDT_ENTRIES_NR PIC_IRQ_MAX + 32 + 1 /* excepts + IRQs + syscall */ |
---|
| 34 | struct cpu_gdt_ptr_s cpu_gdt_ptr __attribute__((aligned(8))); |
---|
| 35 | struct cpu_idt_ptr_s idt_ptr __attribute__((aligned(8))); |
---|
| 36 | struct cpu_tss_s cpu_tss_tbl[CPU_NR] __attribute__((aligned(8))); |
---|
| 37 | struct cpu_gdt_entry_s cpu_gdt[GDT_ENTRIES_NR]; |
---|
| 38 | struct cpu_idt_entry_s cpu_idt[IDT_ENTRIES_NR]; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | void gdt_entry_set(struct cpu_gdt_entry_s *entry, |
---|
| 43 | uint_t base, |
---|
| 44 | uint_t limit, |
---|
| 45 | uint8_t access, |
---|
| 46 | uint8_t granularity) |
---|
| 47 | { |
---|
| 48 | entry->limit_low = (limit & 0xFFFF); |
---|
| 49 | entry->base_low = (base & 0xFFFF); |
---|
| 50 | entry->base_middle = (base >> 16) & 0xFF; |
---|
| 51 | entry->access = access; |
---|
| 52 | entry->granularity = ((granularity & 0xF) << 4) | ((limit >> 16) & 0xF); |
---|
| 53 | entry->base_high = (base >> 24) & 0xFF; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | void gdt_print_entry(struct cpu_gdt_entry_s *entry) |
---|
| 58 | { |
---|
| 59 | printf("limit_low %x, base %x", |
---|
| 60 | entry->limit_low, |
---|
| 61 | (entry->base_high << 24) | (entry->base_middle << 16) | entry->base_low); |
---|
| 62 | |
---|
| 63 | printf("\t acess %x, gran %x\n", |
---|
| 64 | entry->access, |
---|
| 65 | entry->granularity); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void gdt_print(struct cpu_gdt_entry_s *gdt, int nr) |
---|
| 69 | { |
---|
| 70 | int i; |
---|
| 71 | |
---|
| 72 | for(i=0; i < nr; i++) |
---|
| 73 | gdt_print_entry(&gdt[i]); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | void idt_entry_set(struct cpu_idt_entry_s *entry, |
---|
| 78 | uint_t offset, |
---|
| 79 | uint_t selector, |
---|
| 80 | uint8_t flags) |
---|
| 81 | { |
---|
| 82 | entry->offset_low = (offset & 0xFFFF); |
---|
| 83 | entry->selector = selector; |
---|
| 84 | entry->flags = flags; |
---|
| 85 | entry->reserved = 0; |
---|
| 86 | entry->offset_high = offset >> 16; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | extern uint_t __except; |
---|
| 90 | extern uint_t __irq; |
---|
| 91 | |
---|
| 92 | void cpu_idt_init() |
---|
| 93 | { |
---|
| 94 | uint_t i; |
---|
| 95 | volatile uint32_t *ptr; |
---|
| 96 | char *except_start = (char*)&__except; |
---|
| 97 | char *irq_start = (char*)&__irq; |
---|
| 98 | size_t size = (irq_start - except_start) / 32; |
---|
| 99 | |
---|
| 100 | #if 0 |
---|
| 101 | printf("size of exception entry %d, irq_start %x, exep_start %x\n", size, irq_start, except_start); |
---|
| 102 | #endif |
---|
| 103 | |
---|
| 104 | for(i=0; i < 32; i++) |
---|
| 105 | idt_entry_set(&cpu_idt[i], (uint_t)(except_start + i*size), 0x8, 0x8E); |
---|
| 106 | |
---|
| 107 | for(i=0; i < PIC_IRQ_MAX; i++) |
---|
| 108 | { |
---|
| 109 | #if 0 |
---|
| 110 | if(i>=60) |
---|
| 111 | printf("irq%d handler @%x, irq_start %x, i*size = %d\n", |
---|
| 112 | i, (uint_t)(irq_start + i*size), irq_start, i*size); |
---|
| 113 | #endif |
---|
| 114 | idt_entry_set(&cpu_idt[i + 32], (uint_t)(irq_start + i*size), 0x8, 0x8E); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | //while(1); |
---|
| 118 | idt_entry_set(&cpu_idt[i + 32], (uint_t)(irq_start + i*size), 0x8, 0xEF); |
---|
| 119 | |
---|
| 120 | idt_ptr.limit = ((IDT_ENTRIES_NR) * 8) - 1; |
---|
| 121 | idt_ptr.base = (uint32_t)cpu_idt; |
---|
| 122 | ptr = (volatile uint32_t*)(&idt_ptr); |
---|
| 123 | |
---|
| 124 | __asm__ __volatile__ ("" ::: "memory"); |
---|
| 125 | __asm__ volatile ("lidt %0\n" :: "m" (*ptr) ); |
---|
| 126 | // while(1); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void cpu_gdt_init() |
---|
| 130 | { |
---|
| 131 | int i; |
---|
| 132 | |
---|
| 133 | gdt_entry_set(&cpu_gdt[GDT_NULL_ENTRY], 0x0, 0x0, 0x0, 0x0); |
---|
| 134 | |
---|
| 135 | gdt_entry_set(&cpu_gdt[GDT_KTEXT_ENTRY], 0x00, 0xFFFFF, 0x9B, 0x0D); |
---|
| 136 | gdt_entry_set(&cpu_gdt[GDT_KDATA_ENTRY], 0x00, 0xFFFFF, 0x93, 0x0D); |
---|
| 137 | |
---|
| 138 | gdt_entry_set(&cpu_gdt[GDT_UTEXT_ENTRY], 0x00, 0xFFFFF, 0xFF, 0x0D); |
---|
| 139 | gdt_entry_set(&cpu_gdt[GDT_UDATA_ENTRY], 0x00, 0xFFFFF, 0xF3, 0x0D); |
---|
| 140 | // gdt_entry_set(&cpu_gdt[GDT_UTLS_ENTRY], 0x00, 0xFFFFF, 0xF3, 0x0D); |
---|
| 141 | |
---|
| 142 | for(i=GDT_FIXED_NR; i < GDT_ENTRIES_NR; i++) |
---|
| 143 | gdt_entry_set(&cpu_gdt[i],(uint32_t)&cpu_tss_tbl[i - GDT_FIXED_NR], 0x67, 0x89, 0x00); |
---|
| 144 | |
---|
| 145 | cpu_gdt_ptr.limit = ((GDT_ENTRIES_NR) * 8) - 1; |
---|
| 146 | cpu_gdt_ptr.base = (uint32_t)&cpu_gdt[0]; |
---|
| 147 | |
---|
| 148 | #if 0 |
---|
| 149 | printf("cpu_gdt %x\n", val); |
---|
| 150 | printf("cpu_gdt_ptr.base = %x\n", cpu_gdt_ptr.base); |
---|
| 151 | printf("\n\n\n\nentries %d, gdt:\n|", GDT_ENTRIES_NR); |
---|
| 152 | //dump((uint8_t*)&cpu_gdt[0], (GDT_ENTRIES_NR)*8); |
---|
| 153 | //printf("|\n----------------------------------------------\n"); |
---|
| 154 | |
---|
| 155 | printf("gdt_ptr %x:\n|", &cpu_gdt_ptr); |
---|
| 156 | //dump((uint8_t*)&cpu_gdt_ptr, 6); |
---|
| 157 | printf("|\n"); |
---|
| 158 | //while(1); |
---|
| 159 | #endif |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void cpu_set_utls(uint_t addr) |
---|
| 163 | { |
---|
| 164 | gdt_entry_set(&cpu_gdt[GDT_UTLS_ENTRY], addr, 0xFFFFF, 0xF3, 0x0D); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | struct cpu_gdt_ptr_s* cpu_get_gdt_ptr(void) |
---|
| 169 | { |
---|
| 170 | return &cpu_gdt_ptr; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | struct cpu_gdt_entry_s* cpu_get_gdt_entry(int entry) |
---|
| 174 | { |
---|
| 175 | return &cpu_gdt[entry]; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | //#include <kdmsg.h> |
---|
| 179 | struct cpu_tss_s* cpu_get_tss(int cpu_id) |
---|
| 180 | { |
---|
| 181 | //boot_dmsg("cpu_id %x, tss %x\n", cpu_id, &cpu_tss_tbl[cpu_id]); |
---|
| 182 | return &cpu_tss_tbl[cpu_id]; |
---|
| 183 | } |
---|