|
| 1 | +/* gdt.c |
| 2 | + * |
| 3 | + * Copyright (C) 2023 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfBoot. |
| 6 | + * |
| 7 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfBoot is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + * |
| 21 | + */ |
| 22 | +#include <stdint.h> |
| 23 | +#include <x86/gdt.h> |
| 24 | + |
| 25 | +struct segment_descriptor { |
| 26 | + uint16_t seg_limit_15_0; |
| 27 | + uint16_t base_addr_15_0; |
| 28 | + uint8_t base_addr_23_16; |
| 29 | + uint8_t type_s_dpl_p; |
| 30 | + uint8_t seg_limt_19_16_avl_flags; |
| 31 | + uint8_t base_addr_31_24; |
| 32 | +} __attribute__((packed)); |
| 33 | + |
| 34 | +struct gdtr_32 { |
| 35 | + uint16_t limit; |
| 36 | + uint32_t base; |
| 37 | +} __attribute__((packed)); |
| 38 | + |
| 39 | +struct gdtr_64 { |
| 40 | + uint16_t limit; |
| 41 | + uint64_t base; |
| 42 | +} __attribute__((packed)); |
| 43 | + |
| 44 | +#define SEGMENT_DESCRIPTOR_INIT(base, limit, type, s, dpl, p, avl, l, db, g) { \ |
| 45 | + .seg_limit_15_0 = (limit) & 0xffff, \ |
| 46 | + .base_addr_15_0 = (base) & 0xffff, \ |
| 47 | + .base_addr_23_16 = (((base) >> 16) & 0xff), \ |
| 48 | + .type_s_dpl_p = ((type & 0xf) | ((s & 0x1) << 4) | ((dpl & 0x3) << 5) | ((p & 0x1) << 7)), \ |
| 49 | + .seg_limt_19_16_avl_flags = ((((limit) >> 16) & 0xf) | ((avl & 0x1) << 4) | ((l & 0x1) << 5) | ((db & 0x1) << 6) | ((g & 0x1) << 7)), \ |
| 50 | + .base_addr_31_24 = ((base) >> 24) & 0xff, \ |
| 51 | +} |
| 52 | + |
| 53 | +#define GDT_NUM_ENTRIES 5 |
| 54 | + |
| 55 | +struct segment_descriptor gdt[GDT_NUM_ENTRIES] = { |
| 56 | + /* NULL */ |
| 57 | + SEGMENT_DESCRIPTOR_INIT(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
| 58 | + /* Data */ |
| 59 | + SEGMENT_DESCRIPTOR_INIT(0, 0xffffffff, 0x3, 1, 0, 1, 0, 0, 1, 1), |
| 60 | + /* Code (32bit) */ |
| 61 | + SEGMENT_DESCRIPTOR_INIT(0, 0xffffffff, 0xb, 1, 0, 1, 0, 0, 1, 1), |
| 62 | + /* Code (64bit) */ |
| 63 | + SEGMENT_DESCRIPTOR_INIT(0, 0xffffffff, 0xb, 1, 0, 1, 0, 1, 0, 1), |
| 64 | + /* Code (64bit) compat mode */ |
| 65 | + SEGMENT_DESCRIPTOR_INIT(0, 0xffffffff, 0xb, 1, 0, 1, 0, 0, 1, 1), |
| 66 | +}; |
| 67 | + |
| 68 | +int gdt_setup_table(void) |
| 69 | +{ |
| 70 | + struct gdtr_64 gdtr; |
| 71 | + gdtr.limit = sizeof(gdt) - 1; |
| 72 | + gdtr.base = (uint64_t)(uintptr_t)gdt; |
| 73 | + __asm__ volatile ("lgdt %0" : : "m" (gdtr)); |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +int gdt_update_segments(void) |
| 78 | +{ |
| 79 | + __asm__ volatile ( |
| 80 | + "mov %0, %%ax\r\n" |
| 81 | + "mov %%ax, %%ds\r\n" |
| 82 | + "mov %%ax, %%es\r\n" |
| 83 | + "mov %%ax, %%fs\r\n" |
| 84 | + "mov %%ax, %%gs\r\n" |
| 85 | + "mov %%ax, %%ss\r\n" |
| 86 | + "push %1\r\n" |
| 87 | + "lea (seg_cs), %%rax\r\n" |
| 88 | + "push %%rax\r\n" |
| 89 | + "retfq\r\n" |
| 90 | + "seg_cs:\r\n" |
| 91 | + : |
| 92 | + : "i"(GDT_DS), "i" (GDT_CS_64BIT) |
| 93 | + : "rax" |
| 94 | + ); |
| 95 | + return 0; |
| 96 | +} |
0 commit comments