Check-in [ca04ca826c]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:idt, some bugs fixed
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | descriptor
Files: files | file ages | folders
SHA1:ca04ca826c471364c516439f9c953f66bf823380
User & Date: Alisad 2018-11-12 17:38:43
Context
2018-11-12
19:04
bugs fixed check-in: b3b5eb5f08 user: Alisad tags: descriptor
17:38
idt, some bugs fixed check-in: ca04ca826c user: Alisad tags: descriptor
2018-11-05
22:47
gdt moved from interrupts to memory check-in: 3bd065748c user: Alisad tags: descriptor
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to include/asm_shims/shims.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

44
45
46
47
48
49
50





















#pragma once
#include <stdint.h>

extern "C"
{
    static inline void outb(uint16_t port, uint8_t val)
    {
        asm volatile("outb %0, %1"
                     :
                     : "a"(val), "Nd"(port));
    }

    static inline uint8_t inb(uint16_t port)
    {
        uint8_t ret;
        asm volatile("inb %1, %0"
                     : "=a"(ret)
                     : "Nd"(port));
        return ret;
    }

    static inline void outw(uint16_t port, uint16_t val)
    {
        asm volatile("outb %0, %1"
                     :
                     : "a"(val), "Nd"(port));
    }

    static inline uint8_t inw(uint16_t port)
    {
        uint8_t ret;
        asm volatile("inb %1, %0"
                     : "=a"(ret)
                     : "Nd"(port));
        return ret;
    }

    static inline void io_wait(void)
    {
        asm volatile("jmp 1f\n\t"
                     "1:jmp 2f\n\t"
                     "2:");
    }

    static inline void lgdt(void *pointer)
    {
        asm volatile("lgdt (%0)"
                     :
                     : "r"(pointer));
    }
}

























|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
>
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <stdint.h>

extern "C"
{
	static inline void outb(uint16_t port, uint8_t val)
	{
		asm volatile("outb %0, %1"
					 :
					 : "a"(val), "Nd"(port));
	}

	static inline uint8_t inb(uint16_t port)
	{
		uint8_t ret;
		asm volatile("inb %1, %0"
					 : "=a"(ret)
					 : "Nd"(port));
		return ret;
	}

	static inline void outw(uint16_t port, uint16_t val)
	{
		asm volatile("outb %0, %1"
					 :
					 : "a"(val), "Nd"(port));
	}

	static inline uint8_t inw(uint16_t port)
	{
		uint8_t ret;
		asm volatile("inb %1, %0"
					 : "=a"(ret)
					 : "Nd"(port));
		return ret;
	}

	static inline void io_wait(void)
	{
		asm volatile("jmp 1f\n\t"
					 "1:jmp 2f\n\t"
					 "2:");
	}

	static inline void lgdt(void *pointer)
	{
		asm volatile("lgdt (%0)"
					 :
					 : "r"(pointer));
	}

	static inline void sgdt(void *pointer)
	{
		asm volatile("sgdt (%0)"
					 :
					 : "r"(pointer));
	}

	static inline void lidt(void *pointer)
	{
		asm volatile("lidt (%0)"
					 :
					 : "r"(pointer));
	}

	static inline void sidt(void *pointer)
	{
		asm volatile("sidt (%0)"
					 :
					 : "r"(pointer));
	}
}

Changes to include/interrupts/interrupts.h.

1
2

3
4
5
6
7
8
9
#pragma once
#include <memory/gdt.h>

#include "pic_wrapper.h"

namespace INT {class int_handler;}

extern INT::int_handler *interrupts;

namespace INT {

>







1
2
3
4
5
6
7
8
9
10
#pragma once
#include <memory/gdt.h>
#include <memory/idt.h>
#include "pic_wrapper.h"

namespace INT {class int_handler;}

extern INT::int_handler *interrupts;

namespace INT {

Changes to include/memory/gdt.h.

1
2
3
4
5
6
7

8
9
10
11
12
13
14
..
21
22
23
24
25
26
27
28


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
66
67
68
69
70
71
72

73

74
75
76
77
78
79
80
81
82
83
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "asm_shims/shims.h"
#include "libk/memory.h"
#include "libk/string.h"


namespace Paging
{

namespace __GDT
{

................................................................................
	void setbase(uint32_t base);
	uint32_t getlimit();
	void setlimit(uint32_t limit);
	uint8_t getaccess();
	void setaccess(uint8_t access);
	uint8_t getflags();
	void setflags(uint8_t flags);
};



struct GDT
{
	uint16_t size;
	uint32_t offset;
	GDT_ENTRY entries[1];
};

class gdt_iterator
{
	size_t position;
	size_t max_size;
	GDT_ENTRY *table;

  public:
	gdt_iterator(GDT_ENTRY *_table, size_t pos, size_t max_size);
	gdt_iterator(gdt_iterator &other);
	gdt_iterator &operator++(); // postifx
	gdt_iterator operator++(int junk); // prefix
	GDT_ENTRY &operator*();
	bool operator==(gdt_iterator &other);
	bool operator!=(gdt_iterator &other);
................................................................................
	size_t cont = 0;

	void realloc ();

  public:
	gdt_wrapper();
	void load_gdt();

	__GDT::GDT_ENTRY *add_entry(__GDT::GDT_ENTRY entry);

	gdt_wrapper &operator<<(__GDT::GDT_ENTRY entry);
	__GDT::gdt_iterator begin();
	__GDT::gdt_iterator begin() const;

	__GDT::gdt_iterator end();
	__GDT::gdt_iterator end() const;
};


} // namespace Paging






>







 







|
>
>



|
|
|
|







|







 







>

>










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
..
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "asm_shims/shims.h"
#include "libk/memory.h"
#include "libk/string.h"
#include "memory/lma_allocator.h"

namespace Paging
{

namespace __GDT
{

................................................................................
	void setbase(uint32_t base);
	uint32_t getlimit();
	void setlimit(uint32_t limit);
	uint8_t getaccess();
	void setaccess(uint8_t access);
	uint8_t getflags();
	void setflags(uint8_t flags);
	uint64_t getl();
	void setl(uint64_t val);
}__attribute__((packed));

struct GDT
{
	uint16_t size = 0;
	uint32_t offset = 0;
	GDT_ENTRY *entries = 0x00;
}__attribute__((packed));

class gdt_iterator
{
	size_t position;
	size_t max_size;
	GDT_ENTRY *table;

 public:
	gdt_iterator(GDT_ENTRY *_table, size_t pos, size_t max_size);
	gdt_iterator(gdt_iterator &other);
	gdt_iterator &operator++(); // postifx
	gdt_iterator operator++(int junk); // prefix
	GDT_ENTRY &operator*();
	bool operator==(gdt_iterator &other);
	bool operator!=(gdt_iterator &other);
................................................................................
	size_t cont = 0;

	void realloc ();

  public:
	gdt_wrapper();
	void load_gdt();
	void save_gdt();
	__GDT::GDT_ENTRY *add_entry(__GDT::GDT_ENTRY entry);
	__GDT::GDT_ENTRY *get_entry(int index);
	gdt_wrapper &operator<<(__GDT::GDT_ENTRY entry);
	__GDT::gdt_iterator begin();
	__GDT::gdt_iterator begin() const;

	__GDT::gdt_iterator end();
	__GDT::gdt_iterator end() const;
};


} // namespace Paging

Added include/memory/idt.h.










































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "asm_shims/shims.h"
#include "libk/memory.h"
#include "libk/string.h"
#include "memory/lma_allocator.h"

namespace Paging
{

namespace __IDT
{

struct IDT_ENTRY
{
	private: uint8_t _Table[8];

	public:
	uint32_t getoffset();
	void setoffset(uint32_t offset);
	uint16_t getselector();
	void setselector(uint16_t selector);
	uint8_t getaccess();
	void setaccess(uint8_t access);
	uint64_t getl();
	void setl(uint64_t val);
}__attribute__((packed));

struct IDT
{
	uint16_t size = 0;
	uint32_t offset = 0;
	IDT_ENTRY* entries = 0x00;
}__attribute__((packed));

class idt_iterator
{
	size_t position;
	size_t max_size;
	IDT_ENTRY *table;

  public:
	idt_iterator(IDT_ENTRY *_table, size_t pos, size_t max_size);
	idt_iterator(idt_iterator &other);
	idt_iterator &operator++(); // postifx
	idt_iterator operator++(int junk); // prefix
	IDT_ENTRY &operator*();
	bool operator==(idt_iterator &other);
	bool operator!=(idt_iterator &other);
};

IDT_ENTRY construct_empty_descriptor();
IDT_ENTRY construct_trap_gate(uint16_t selector, uint32_t offset, uint8_t dpl, bool present, bool sizeofgate);
IDT_ENTRY construct_interrupt_gate(uint16_t selector, uint32_t offset, uint8_t dpl, bool present, bool sizeofgate);
IDT_ENTRY construct_task_gate(uint16_t selector, uint8_t dpl, bool present);

}  // namespace __IDT
class idt_wrapper
{
	__IDT::IDT *table;
	bool is_loaded;

	size_t prealloc = 0;
	size_t cont = 0;

	void realloc ();

  public:
	idt_wrapper();
	void load_idt();
	void save_idt();
	__IDT::IDT_ENTRY *add_entry(__IDT::IDT_ENTRY entry);
	__IDT::IDT_ENTRY *get_entry(int index);
	idt_wrapper &operator<<(__IDT::IDT_ENTRY entry);
	__IDT::idt_iterator begin();
	__IDT::idt_iterator begin() const;

	__IDT::idt_iterator end();
	__IDT::idt_iterator end() const;
};


} // namespace Paging

Changes to src/init.cpp.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
}

extern "C"
{
	void kernel_main(multiboot_info_t *mboot, uint32_t magic)
	{
		INIT::initialize(mboot, magic);

		INIT::display_greeting();
		INIT::display_mem_info(Paging::Physical::physical_memory);

		TH::TESTS_REGISTER();
		//Paging::__GDT::GDT_ENTRY entry = Paging::__GDT::construct_gdt_entry(0, 0xFFFFF, 0x90, 0x92);

		//Paging::gdt_wrapper gw;
		//gw.add_entry(entry);
		//gw.load_gdt();
	}
}






|




<
|
<
<
<
|
<
6
7
8
9
10
11
12
13
14
15
16
17

18



19

}

extern "C"
{
	void kernel_main(multiboot_info_t *mboot, uint32_t magic)
	{
		INIT::initialize(mboot, magic);
		Termutils::cout << Termutils::terminal_output::int_mode::hex;
		INIT::display_greeting();
		INIT::display_mem_info(Paging::Physical::physical_memory);

		TH::TESTS_REGISTER();

	}



}

Changes to src/interrupts/interrupts.cpp.

1
2
3
4
5
6
7
8



9

10
11
12
13
14
15
#include "interrupts/interrupts.h"

INT::int_handler* interrupts = nullptr;

namespace INT {

   int_handler::int_handler () : gdt(new Paging::gdt_wrapper()) {
       //gdt.load_gdt();



   }


   void initialize () {
       interrupts = new int_handler();
   }

}





|
|
>
>
>
|
>






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "interrupts/interrupts.h"

INT::int_handler* interrupts = nullptr;

namespace INT {

   //int_handler::int_handler () : gdt(new Paging::gdt_wrapper()) {
   //    //gdt.load_gdt();
   //}
	int_handler::int_handler()
	{

	}

   void initialize () {
       interrupts = new int_handler();
   }

}

Changes to src/memory/gdt.cpp.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
..
47
48
49
50
51
52
53
54









55
56
57
58
59
60
61
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
...
126
127
128
129
130
131
132
133
134


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

151
152
153
154
155
156
157
158
159

160

















161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179






180
181
182
183
184
185
186
187
188
189
	return (static_cast<uint32_t>(_Table[2])) + (static_cast<uint32_t>(_Table[3]) << 8)
		+ (static_cast<uint32_t>(_Table[4]) << 16) + (static_cast<uint32_t>(_Table[7]) << 24);
}

void GDT_ENTRY::setbase(uint32_t base)
{
	_Table[3] = static_cast<uint8_t>(base);
	_Table[2] = static_cast<uint8_t>(8 >> base);
	_Table[4] = static_cast<uint8_t>(16 >> base);
	_Table[7] = static_cast<uint8_t>(24 >> base);
}

uint32_t GDT_ENTRY::getlimit()
{
	return (static_cast<uint32_t>(_Table[0])) + (static_cast<uint32_t>(_Table[1]) << 8)
		+ ((static_cast<uint32_t>(_Table[6]) << 16) & 0x0F);
}

void GDT_ENTRY::setlimit(uint32_t limit)
{
	_Table[1] = static_cast<uint8_t>(limit);
	_Table[0] = static_cast<uint8_t>(8 >> limit);
	_Table[6] = (_Table[6] & 0xF0) | (static_cast<uint8_t>(16 >> limit) & 0x0F);
}

uint8_t GDT_ENTRY::getaccess()
{
	return _Table[5];
}

................................................................................
uint8_t GDT_ENTRY::getflags()
{
	return _Table[6] & 0xF0;
}

void GDT_ENTRY::setflags(uint8_t flags)
{
	_Table[6] = flags & 0xF0;









}

gdt_iterator::gdt_iterator(GDT_ENTRY *_table, size_t pos, size_t max)
   : table(_table), position (pos), max_size (max)
{

}
................................................................................
	if (position >= max_size)
		position = 0;
	return *this;
}

gdt_iterator gdt_iterator::operator++(int junk)
{
   gdt_iterator a = gdt_iterator(*this);
	position++;
	if ((++position) >= max_size)
		position = 0;
	return *this;
}

GDT_ENTRY &gdt_iterator::operator*()
................................................................................
{
	return construct_gdt_entry(0, 0, 0, 0);
}
} // namespace __GDT

gdt_wrapper::gdt_wrapper()
{
	Termutils::cout << "constructor \n";
	table = new __GDT::GDT;


	table->size = 6;
	table->offset = (int)table;
	table->size--;
	Termutils::cout << "setup \n";

	add_entry(__GDT::construct_empty_descriptor());
	Termutils::cout << "created descriptor \n";

}
void gdt_wrapper::realloc()
{
	Termutils::cout << "Reallocating \n";
	prealloc += 10;
	void *new_space = malloc((sizeof(__GDT::GDT_ENTRY) * prealloc) + sizeof(uint16_t) + sizeof(uint32_t));
	memcpy(table, new_space, cont * sizeof (__GDT::GDT_ENTRY));
	for (size_t i = 0; i < prealloc; i++) {

		delete &table->entries[i];
	}
	delete table;
	table = (__GDT::GDT *)new_space;
}
void gdt_wrapper::load_gdt()
{
	Termutils::cout << "before lgdt:";
	lgdt(table);

	Termutils::cout << "after lgdt\n";

















}
__GDT::GDT_ENTRY *gdt_wrapper::add_entry(__GDT::GDT_ENTRY entry)
{

	Termutils::cout << "Adding entry\n";

	if (cont == prealloc)
		realloc();

	//Termutils::cout << "reallocated\n";

	table->entries[cont] = entry;
	//Termutils::cout << "wrote entry\n";
	cont++;
	//Termutils::cout << "increased size\n";
	table->size += 8;
	Termutils::cout << "increased table size \n";
	return &table->entries[cont-1];
}






gdt_wrapper& gdt_wrapper::operator<< (__GDT::GDT_ENTRY entry) {
	add_entry (entry);
	return *this;
}
__GDT::gdt_iterator gdt_wrapper::begin () {
	return __GDT::gdt_iterator (&table->entries [0], 0, cont);
}

__GDT::gdt_iterator gdt_wrapper::begin () const {
	return __GDT::gdt_iterator (&table->entries [0], 0, cont);






|
|
|











|
|







 







|
>
>
>
>
>
>
>
>
>







 







|







 







|
|
>
>
|
|
<
<


|
<



<
|
|
|
|
>


|
|



<

>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
<
<



<
<

<

<

<
|

>
>
>
>
>
>

|
<







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
..
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
135
136
137
138
139
140
141
142
143
144
145
146
147


148
149
150

151
152
153

154
155
156
157
158
159
160
161
162
163
164
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188



189
190
191


192

193

194

195
196
197
198
199
200
201
202
203
204

205
206
207
208
209
210
211
	return (static_cast<uint32_t>(_Table[2])) + (static_cast<uint32_t>(_Table[3]) << 8)
		+ (static_cast<uint32_t>(_Table[4]) << 16) + (static_cast<uint32_t>(_Table[7]) << 24);
}

void GDT_ENTRY::setbase(uint32_t base)
{
	_Table[3] = static_cast<uint8_t>(base);
	_Table[2] = static_cast<uint8_t>(base >> 8);
	_Table[4] = static_cast<uint8_t>(base >> 16);
	_Table[7] = static_cast<uint8_t>(base >> 24);
}

uint32_t GDT_ENTRY::getlimit()
{
	return (static_cast<uint32_t>(_Table[0])) + (static_cast<uint32_t>(_Table[1]) << 8)
		+ ((static_cast<uint32_t>(_Table[6]) << 16) & 0x0F);
}

void GDT_ENTRY::setlimit(uint32_t limit)
{
	_Table[1] = static_cast<uint8_t>(limit);
	_Table[0] = static_cast<uint8_t>(limit >> 8);
	_Table[6] = (_Table[6] & 0xF0) | (static_cast<uint8_t>(limit >> 16) & 0x0F);
}

uint8_t GDT_ENTRY::getaccess()
{
	return _Table[5];
}

................................................................................
uint8_t GDT_ENTRY::getflags()
{
	return _Table[6] & 0xF0;
}

void GDT_ENTRY::setflags(uint8_t flags)
{
	_Table[6] = (flags & 0xF0) | (_Table[6] & 0x0F);
}

uint64_t GDT_ENTRY::getl()
{
	return *reinterpret_cast<uint64_t *>(_Table);
}
void GDT_ENTRY::setl(uint64_t val)
{
	*reinterpret_cast<uint64_t *>(_Table) = val;
}

gdt_iterator::gdt_iterator(GDT_ENTRY *_table, size_t pos, size_t max)
   : table(_table), position (pos), max_size (max)
{

}
................................................................................
	if (position >= max_size)
		position = 0;
	return *this;
}

gdt_iterator gdt_iterator::operator++(int junk)
{
	gdt_iterator a = gdt_iterator(*this);
	position++;
	if ((++position) >= max_size)
		position = 0;
	return *this;
}

GDT_ENTRY &gdt_iterator::operator*()
................................................................................
{
	return construct_gdt_entry(0, 0, 0, 0);
}
} // namespace __GDT

gdt_wrapper::gdt_wrapper()
{
	table = reinterpret_cast<__GDT::GDT *>(Physical::LMA::allocate(sizeof(__GDT::GDT)));
	*table = __GDT::GDT();

	table->entries = reinterpret_cast<__GDT::GDT_ENTRY *>(Physical::LMA::allocate(0 * sizeof(__GDT::GDT_ENTRY)));
	table->size = 0;
	table->offset = reinterpret_cast<uint32_t>((&table->entries));



	add_entry(__GDT::construct_empty_descriptor());
	table->size--;

}
void gdt_wrapper::realloc()
{

	prealloc += 16;
	void *new_space = Physical::LMA::allocate((sizeof(__GDT::GDT_ENTRY) * prealloc));
	memcpy(table->entries, new_space, cont * sizeof (__GDT::GDT_ENTRY));
	for (size_t i = 0; i < prealloc; i++)
	{
		delete &table->entries[i];
	}
	table->entries = reinterpret_cast<__GDT::GDT_ENTRY *>(new_space);
	table->offset = reinterpret_cast<uint32_t>(&table->entries) + 6;
}
void gdt_wrapper::load_gdt()
{

	lgdt(table);
}
void gdt_wrapper::save_gdt()
{
	uint16_t *p;
	sgdt(p);

	uint16_t _limit = p[0];
	uint32_t _base = p[1];

	uint64_t *tmp = reinterpret_cast<uint64_t *>(_base);
	__GDT::GDT_ENTRY tmpg;
	for (size_t index = 1; index < _limit/8; index++)
	{
		tmpg.setl(tmp[index]);
		if (cont > index)
			table->entries[index] = tmpg;
		else
			add_entry(tmpg);
	}
}
__GDT::GDT_ENTRY *gdt_wrapper::add_entry(__GDT::GDT_ENTRY entry)
{



	if (cont == prealloc)
		realloc();



	table->entries[cont] = entry;

	cont++;

	table->size += 8;

	return &table->entries[cont - 1];
}

__GDT::GDT_ENTRY *gdt_wrapper::get_entry(int index)
{
	return &table->entries[index];
}

gdt_wrapper& gdt_wrapper::operator<< (__GDT::GDT_ENTRY entry) {
	add_entry (entry); return *this;

}
__GDT::gdt_iterator gdt_wrapper::begin () {
	return __GDT::gdt_iterator (&table->entries [0], 0, cont);
}

__GDT::gdt_iterator gdt_wrapper::begin () const {
	return __GDT::gdt_iterator (&table->entries [0], 0, cont);

Added src/memory/idt.cpp.


























































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "memory/idt.h"
#include <termutils/terminal.h>

namespace Paging
{

namespace __IDT
{

uint32_t IDT_ENTRY::getoffset()
{
	return (static_cast<uint32_t>(_Table[1])) + (static_cast<uint32_t>(_Table[0]) << 8)
		+ (static_cast<uint32_t>(_Table[7]) << 16) + (static_cast<uint32_t>(_Table[6]) << 24);
}

void IDT_ENTRY::setoffset(uint32_t offset)
{
	_Table[1] = static_cast<uint8_t>(offset);
	_Table[0] = static_cast<uint8_t>(offset >> 8);
	_Table[7] = static_cast<uint8_t>(offset >> 16);
	_Table[6] = static_cast<uint8_t>(offset >> 24);
}

uint16_t IDT_ENTRY::getselector()
{
	return (static_cast<uint16_t>(_Table[3])) + (static_cast<uint16_t>(_Table[2]) << 8);
}

void IDT_ENTRY::setselector(uint16_t selector)
{
	_Table[3] = static_cast<uint8_t>(selector);
	_Table[2] = static_cast<uint8_t>(selector >> 8);
}

uint8_t IDT_ENTRY::getaccess()
{
	return _Table[5];
}

void IDT_ENTRY::setaccess(uint8_t access)
{
	_Table[5] = access;
}

uint64_t IDT_ENTRY::getl()
{
	return *reinterpret_cast<uint64_t *>(_Table);
}

void IDT_ENTRY::setl(uint64_t val)
{
	*reinterpret_cast<uint64_t *>(_Table) = val;
}

idt_iterator::idt_iterator(IDT_ENTRY *_table, size_t pos, size_t max)
   : table(_table), position (pos), max_size (max)
{

}

idt_iterator::idt_iterator(idt_iterator &other)
   : position(other.position), table(other.table), max_size(other.max_size)
{

}

idt_iterator &idt_iterator::operator++()
{
	position++;
	if (position >= max_size)
		position = 0;
	return *this;
}

idt_iterator idt_iterator::operator++(int junk)
{
	idt_iterator a = idt_iterator(*this);
	position++;
	if ((++position) >= max_size)
		position = 0;
	return *this;
}

IDT_ENTRY &idt_iterator::operator*()
{
	return table[position];
}

bool idt_iterator::operator==(idt_iterator &other)
{
	return (other.position == position) && (other.table == table);
}

bool idt_iterator::operator!=(idt_iterator &other)
{
	return !(*this == other);
}

IDT_ENTRY construct_trap_gate(uint16_t selector, uint32_t offset, uint8_t dpl, bool present, bool sizeofgate)
{
	IDT_ENTRY tmp;

	tmp.setselector(selector);
	tmp.setoffset(offset);
	tmp.setaccess((present << 7) | ((dpl & 0x03) << 5) | (sizeofgate << 3) | 0x07);

	return tmp;
}

IDT_ENTRY construct_interrupt_gate(uint16_t selector, uint32_t offset, uint8_t dpl, bool present, bool sizeofgate)
{
	IDT_ENTRY tmp;

	tmp.setselector(selector);
	tmp.setoffset(offset);
	tmp.setaccess((present << 7) | ((dpl & 0x03) << 5) | (sizeofgate << 3) | 0x06);

	return tmp;
}

IDT_ENTRY construct_task_gate(uint16_t selector, uint8_t dpl, bool present)
{
	IDT_ENTRY tmp;

	tmp.setselector(selector);
	tmp.setaccess((present << 7) | ((dpl & 0x03) << 5) | 0x05);

	return tmp;
}

IDT_ENTRY construct_empty_descriptor()
{
	IDT_ENTRY tmp;

	tmp.setl(0x0000000000000000);

	return tmp;
}
} // namespace __IDT

idt_wrapper::idt_wrapper()
{
	table = (__IDT::IDT *) Physical::LMA::allocate(sizeof(__IDT::IDT));
	*table = __IDT::IDT();

	table->entries = reinterpret_cast<__IDT::IDT_ENTRY *>(Physical::LMA::allocate(0 * sizeof(__IDT::IDT_ENTRY)));
	table->size = 0;
	table->offset = reinterpret_cast<uint32_t>((&table->entries));

	add_entry(__IDT::construct_empty_descriptor());
	table->size--;
}
void idt_wrapper::realloc()
{
	prealloc += 16;
	void *new_space = Physical::LMA::allocate((sizeof(__IDT::IDT_ENTRY) * prealloc));
	memcpy(table->entries, new_space, cont * sizeof (__IDT::IDT_ENTRY));
	for (size_t i = 0; i < prealloc; i++)
	{
		delete &table->entries[i];
	}
	table->entries = reinterpret_cast<__IDT::IDT_ENTRY *>(new_space);
	table->offset = reinterpret_cast<uint32_t>((&table->entries)) + 6;
}
void idt_wrapper::load_idt()
{
	lidt(table);
}
void idt_wrapper::save_idt()
{
	uint16_t *p;
	sidt(p);

	uint16_t _limit = p[0];
	uint32_t _base = p[1];

	uint64_t *tmp = reinterpret_cast<uint64_t *>(_base);
	__IDT::IDT_ENTRY tmpg;
	for (size_t index = 1; index < _limit/8; index++)
	{
		tmpg.setl(tmp[index]);
		if (cont > index)
			table->entries[index] = tmpg;
		else
			add_entry(tmpg);
	}
}
__IDT::IDT_ENTRY *idt_wrapper::add_entry(__IDT::IDT_ENTRY entry)
{
	if (cont == prealloc)
		realloc();

	table->entries[cont] = entry;
	cont++;
	table->size += 8;
	return &table->entries[cont - 1];
}

__IDT::IDT_ENTRY *idt_wrapper::get_entry(int index)
{
	return &table->entries[index];
}

idt_wrapper& idt_wrapper::operator<< (__IDT::IDT_ENTRY entry) {
	add_entry (entry);
	return *this;
}
__IDT::idt_iterator idt_wrapper::begin () {
	return __IDT::idt_iterator (&table->entries [0], 0, cont);
}

__IDT::idt_iterator idt_wrapper::begin () const {
	return __IDT::idt_iterator (&table->entries [0], 0, cont);
}
__IDT::idt_iterator idt_wrapper::end () {
	return __IDT::idt_iterator (&table->entries [cont], cont, cont);
}
__IDT::idt_iterator idt_wrapper::end () const {
	return __IDT::idt_iterator (&table->entries [cont], cont, cont);
}
} // namespace INT

Changes to src/tests/__register_tests.cpp.

28
29
30
31
32
33
34

35
36
37
38
39
40
41
42
43

44
45
46
47
48
	return run_test(fn, ksdk::string_view((char *)str, strlen(str)));
}

extern bool sosa_tree_test();
extern bool ata_test();
extern bool acpi_test();
extern bool lma_alloc_test();


void TESTS_REGISTER()
{
	if constexpr (config::test_build)
	{
		run_test(sosa_tree_test, "SOSA Tree datastructure");
		run_test(ata_test, "(S)ATA-DRIVES");
		run_test(acpi_test, "ACPI");
		run_test(lma_alloc_test, "LMA Allocator");


		Termutils::cout << runned << " test done, " << runned - success << " failures" << Termutils::endl;
	}
}
} // namespace TH






>









>





28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	return run_test(fn, ksdk::string_view((char *)str, strlen(str)));
}

extern bool sosa_tree_test();
extern bool ata_test();
extern bool acpi_test();
extern bool lma_alloc_test();
extern bool gdt_test();

void TESTS_REGISTER()
{
	if constexpr (config::test_build)
	{
		run_test(sosa_tree_test, "SOSA Tree datastructure");
		run_test(ata_test, "(S)ATA-DRIVES");
		run_test(acpi_test, "ACPI");
		run_test(lma_alloc_test, "LMA Allocator");
		run_test(gdt_test, "GDT");

		Termutils::cout << runned << " test done, " << runned - success << " failures" << Termutils::endl;
	}
}
} // namespace TH

Added src/tests/gdt_test.cpp.


















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "memory/gdt.h"
#include "memory/idt.h"
#include "termutils/terminal.h"
#include "memory/lma_allocator.h"

namespace TH
{
bool gdt_test()
{
	Termutils::cout << "session: \n";
	Paging::gdt_wrapper *g1;
	g1 = reinterpret_cast<Paging::gdt_wrapper *>(Paging::Physical::LMA::allocate(sizeof(Paging::gdt_wrapper)));
	*g1 = Paging::gdt_wrapper();
	g1->save_gdt();
	g1->add_entry(Paging::__GDT::construct_gdt_entry(0x12345678,0xabcd, 0x90, 0x92));
	g1->load_gdt();

	Paging::idt_wrapper *g2;
	g2 = reinterpret_cast<Paging::idt_wrapper *>(Paging::Physical::LMA::allocate(sizeof(Paging::idt_wrapper)));
	*g2 = Paging::idt_wrapper();
	g2->add_entry(Paging::__IDT::construct_trap_gate(0xaaaa, 0xbbbbbbbb, 0x00, 1, 1));
	g2->load_idt();
	return true;
}
} // namespace TH