Permalink
Browse files

Files for part 3, https://www.youtube.com/watch?v=pfWjteMpcxE

  • Loading branch information...
Viktor Engelmann Viktor Engelmann
Viktor Engelmann authored and Viktor Engelmann committed Jul 4, 2016
1 parent b440da7 commit fd95a895c3c8060fc3e13bef0337c6b2645cf856
Showing with 146 additions and 1 deletion.
  1. +101 −0 gdt.cpp
  2. +41 −0 gdt.h
  3. +3 −0 kernel.cpp
  4. +1 −1 makefile
View
101 gdt.cpp
@@ -0,0 +1,101 @@
#include "gdt.h"
GlobalDescriptorTable::GlobalDescriptorTable()
: nullSegmentSelector(0, 0, 0),
unusedSegmentSelector(0, 0, 0),
codeSegmentSelector(0, 64*1024*1024, 0x9A),
dataSegmentSelector(0, 64*1024*1024, 0x92)
{
uint32_t i[2];
i[1] = (uint32_t)this;
i[0] = sizeof(GlobalDescriptorTable) << 16;
asm volatile("lgdt (%0)": :"p" (((uint8_t *) i)+2));
}
GlobalDescriptorTable::~GlobalDescriptorTable()
{
}
uint16_t GlobalDescriptorTable::DataSegmentSelector()
{
return (uint8_t*)&dataSegmentSelector - (uint8_t*)this;
}
uint16_t GlobalDescriptorTable::CodeSegmentSelector()
{
return (uint8_t*)&codeSegmentSelector - (uint8_t*)this;
}
GlobalDescriptorTable::SegmentDescriptor::SegmentDescriptor(uint32_t base, uint32_t limit, uint8_t type)
{
uint8_t* target = (uint8_t*)this;
if (limit <= 65536)
{
// 16-bit address space
target[6] = 0x40;
}
else
{
// 32-bit address space
// Now we have to squeeze the (32-bit) limit into 2.5 regiters (20-bit).
// This is done by discarding the 12 least significant bits, but this
// is only legal, if they are all ==1, so they are implicitly still there
// so if the last bits aren't all 1, we have to set them to 1, but this
// would increase the limit (cannot do that, because we might go beyond
// the physical limit or get overlap with other segments) so we have to
// compensate this by decreasing a higher bit (and might have up to
// 4095 wasted bytes behind the used memory)
if((limit & 0xFFF) != 0xFFF)
limit = (limit >> 12)-1;
else
limit = limit >> 12;
target[6] = 0xC0;
}
// Encode the limit
target[0] = limit & 0xFF;
target[1] = (limit >> 8) & 0xFF;
target[6] |= (limit >> 16) & 0xF;
// Encode the base
target[2] = base & 0xFF;
target[3] = (base >> 8) & 0xFF;
target[4] = (base >> 16) & 0xFF;
target[7] = (base >> 24) & 0xFF;
// Type
target[5] = type;
}
uint32_t GlobalDescriptorTable::SegmentDescriptor::Base()
{
uint8_t* target = (uint8_t*)this;
uint32_t result = target[7];
result = (result << 8) + target[4];
result = (result << 8) + target[3];
result = (result << 8) + target[2];
return result;
}
uint32_t GlobalDescriptorTable::SegmentDescriptor::Limit()
{
uint8_t* target = (uint8_t*)this;
uint32_t result = target[6] & 0xF;
result = (result << 8) + target[1];
result = (result << 8) + target[0];
if((target[6] & 0xC0) == 0xC0)
result = (result << 12) | 0xFFF;
return result;
}
View
41 gdt.h
@@ -0,0 +1,41 @@
#ifndef __GDT_H
#define __GDT_H
#include "types.h"
class GlobalDescriptorTable
{
public:
class SegmentDescriptor
{
private:
uint16_t limit_lo;
uint16_t base_lo;
uint8_t base_hi;
uint8_t type;
uint8_t limit_hi;
uint8_t base_vhi;
public:
SegmentDescriptor(uint32_t base, uint32_t limit, uint8_t type);
uint32_t Base();
uint32_t Limit();
} __attribute__((packed));
private:
SegmentDescriptor nullSegmentSelector;
SegmentDescriptor unusedSegmentSelector;
SegmentDescriptor codeSegmentSelector;
SegmentDescriptor dataSegmentSelector;
public:
GlobalDescriptorTable();
~GlobalDescriptorTable();
uint16_t CodeSegmentSelector();
uint16_t DataSegmentSelector();
};
#endif
View
@@ -1,5 +1,6 @@
#include "types.h"
#include "gdt.h"
void printf(char* str)
{
@@ -26,5 +27,7 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
{
printf("Hello World! --- http://www.AlgorithMan.de");
GlobalDescriptorTable gdt;
while(1);
}
View
@@ -6,7 +6,7 @@ GCCPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-excep
ASPARAMS = --32
LDPARAMS = -melf_i386
objects = loader.o kernel.o
objects = loader.o gdt.o kernel.o

0 comments on commit fd95a89

Please sign in to comment.