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
|
#include "seg.h"
#include "mem.h"
#include <core/sys.h>
#include <lib/cpp.h>
segment::segment() {
mappings = 0;
}
segment::~segment() {
}
/* Call this function when mapping a segment to a page directory.
Calls the appropriate map method and updates the segment's and pagedir's information. */
segment_map *segment::map(page_directory *pagedir, size_t offset) {
segment_map *sm = i_map(pagedir, offset);
if (sm == 0) return 0;
mappings++;
sm->seg = this;
sm->pagedir = pagedir;
sm->next = pagedir->mappedSegs;
pagedir->mappedSegs = sm;
return sm;
}
/* Call this function when unmapping a segment from a page directory.
The segment will automatically be deleted if it is not mapped.
Calls the appropriate unmap method and updates the segment's and pagedir's information. */
void segment::unmap(segment_map *map) {
ASSERT(this == map->seg);
i_unmap(map);
if (map->pagedir->mappedSegs == map) {
map->pagedir->mappedSegs = map->next;
} else {
segment_map *m = map->pagedir->mappedSegs;
while (m->next != 0 && m->next != map) m = m->next;
if (m->next == map) m->next = map->next;
}
delete map;
mappings--;
if (mappings == 0) {
delete this;
}
}
// ************************************ SIMPLESEG stuff *************
/* Call this when creating a simpleseg.
Creates the simpleseg structure and the segment structure and fills them up. */
simpleseg::simpleseg(size_t s, size_t l, int w) : segment() {
writable = w;
start = s;
len = l;
}
simpleseg::~simpleseg() {
}
/* For internal use only. Called when a simpleseg is mapped to a pagedirectory. */
segment_map* simpleseg::i_map(page_directory* pagedir, size_t offset) {
segment_map *sm = new segment_map();
sm->start = start;
sm->len = len;
return sm;
}
/* For internal use only. Called when a simpleseg is unmapped.
Frees all the allocated pages. */
void simpleseg::i_unmap(segment_map* sm) {
size_t i;
for (i = sm->start; i < sm->start + sm->len; i += 0x1000) {
page_unmapFree(pagedir_getPage(sm->pagedir, i, 0));
}
}
/* For internal use only. Handles a page fault. Can allocate and map a frame if necessary. */
int simpleseg::handle_fault(segment_map* sm, size_t addr, int write) {
ASSERT(sm->seg == this);
if (write && !writable) return 1;
addr &= 0xFFFFF000;
page *p = pagedir_getPage(sm->pagedir, addr, 1);
if (p->frame != 0) return 1;
page_map(p, frame_alloc(), 1, writable);
return 0;
}
/* Call this to resize a simpleseg. Ajusts the size and frees pages if the new size is smaller.*/
int simpleseg::resize(segment_map *map, size_t nlen) {
ASSERT(this == map->seg);
size_t i;
if (map == 0) return -1;
if (nlen & 0xFFF) nlen = (nlen & 0xFFFFF000) + 0x1000;
if (nlen < map->len) {
for (i = map->start + nlen; i < map->start + map->len; i += 0x1000) {
page_unmapFree(pagedir_getPage(map->pagedir, i, 0));
}
map->len = nlen;
len = nlen;
} else if (nlen > map->len) {
map->len = nlen;
len = nlen;
}
return 0;
}
|