blob: 06209a111f8d3151a8f62cdebb0c1415ff38a2f7 (
plain) (
tree)
|
|
#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;
}
|