⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdt.c

📁 一个操作系统的源代码
💻 C
字号:
/** gdt.c ** ** Original Author: Kasper Verdich Lund ** Date: 10/11/99 ** ** Description: ** Global Descriptor Table functions ** ** This program is free software, you can redistribute it and/or ** modify it under the terms of the GNU General Public License ** as published by the Free Software Foundation; either version ** 2 of the License, or (at your option) any later version. ** ** This program is distributed in the hope that it will be ** useful, but WITHOUT ANY WARRANTY; without even the implied ** warranty or MERCHANTABILITY or FITNESS FOR A PARTICULAR ** PURPOSE.  See the GNU General Public License for more ** details. ** ** You should have received a copy of the GNU General Public ** License along with this program; if not, write to the ** Free Software Foundation, Inc., 59 Temple Place, Suite 330, ** Boston, MA 02111-1307 USA ** *********************************************************Apostle OS**/#include <gdt.h>#include <tss.h>#include <mem.h>#include <types.h>#include <string.h>descriptor *GDT = (descriptor *)0;void setupGDT(void){  /* it is assumed that GDT[1] is    * the flat code segment descriptor    * and that GDT[2] is the flat data   * segment descriptor */  dword GDTR[2];  memset((void *)GDT, 0, GDT_SIZE * sizeof(descriptor));  setupDescriptor(&GDT[1], 0, LIMIT_4GB, 8, 0, 1, 1); /* 0x08 */  setupDescriptor(&GDT[2], 0, LIMIT_4GB, 2, 0, 1, 1); /* 0x10 */  setupDescriptor(&GDT[3], 0, LIMIT_4GB, 8, 3, 1, 1); /* 0x1B */  setupDescriptor(&GDT[4], 0, LIMIT_4GB, 2, 3, 1, 1); /* 0x23 */    /* the following computation of the GDTR   * could/should be done much more readable by   * describing the memory layout of the GDTR   * in a packed struct in gdt.h */  GDTR[0] = ((GDT_SIZE * sizeof(descriptor)) - 1) << 16;  GDTR[1] = (dword)GDT;  /* load the GDTR - it's only 48-bits and therefore the    * two first bytes are not used */  __asm__("lgdt (%0)" : : "r" (((byte *)GDTR) + 2));    /* flush the instruction queue and set CS */  __asm__("ljmp $8, $__flush\n"          "__flush:");  /* set DS, SS, ES, FS, GS */  __asm__("movl $0x10, %eax\n"          "movw %ax, %ss\n"          "movl $0x23, %eax\n"          "movw %ax, %ds\n"          "movw %ax, %es\n"          "movw %ax, %fs\n"          "movw %ax, %gs");}void setupDescriptor(descriptor *d, dword base, dword limit, byte type, byte dpl, byte s, byte db){  /* calculate limit and granularity */  if (limit > LIMIT_1MB)     {      limit = limit >> 12;      d->g = 1;    }  else    d->g = 0;  /* set base address */  d->base0_15 = base;  d->base16_23 = base >> 16;  d->base24_31 = base >> 24;    /* set segment limit */  d->limit0_15 = limit;  d->limit16_19 = limit >> 16;    /* set rest of descriptor */  d->type = type;  d->s = s;  d->dpl = dpl;  d->p = 1;  d->avl = 0;  d->zero = 0;  d->db = db; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -