Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions kernel/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ static void *mem_ptr_nofault(struct mem *mem, addr_t addr, int type) {
return entry->data->data + entry->offset + PGOFFSET(addr);
}

static void mem_upgrade_to_write(struct mem *mem) {
read_wrunlock(&mem->lock);
write_wrlock(&mem->lock);
}

static void mem_downgrade_to_read(struct mem *mem) {
write_wrunlock(&mem->lock);
read_wrlock(&mem->lock);
}

void *mem_ptr(struct mem *mem, addr_t addr, int type) {
void *old_ptr = mem_ptr_nofault(mem, addr, type); // just for an assert

Expand All @@ -251,15 +261,9 @@ void *mem_ptr(struct mem *mem, addr_t addr, int type) {

// Changing memory maps must be done with the write lock. But this is
// called with the read lock.
// This locking stuff is copy/pasted for all the code in this function
// which changes memory maps.
// TODO: factor the lock/unlock code here into a new function. Do this
// next time you touch this function.
read_wrunlock(&mem->lock);
write_wrlock(&mem->lock);
mem_upgrade_to_write(mem);
pt_map_nothing(mem, page, 1, P_WRITE | P_GROWSDOWN);
write_wrunlock(&mem->lock);
read_wrlock(&mem->lock);
mem_downgrade_to_read(mem);

entry = mem_pt(mem, page);
}
Expand All @@ -280,13 +284,10 @@ void *mem_ptr(struct mem *mem, addr_t addr, int type) {
void *copy = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);

// copy/paste from above
read_wrunlock(&mem->lock);
write_wrlock(&mem->lock);
mem_upgrade_to_write(mem);
memcpy(copy, data, PAGE_SIZE);
pt_map(mem, page, 1, copy, 0, entry->flags &~ P_COW);
write_wrunlock(&mem->lock);
read_wrlock(&mem->lock);
mem_downgrade_to_read(mem);
}
}

Expand Down
4 changes: 3 additions & 1 deletion kernel/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ int_t sys_timer_create(dword_t clock, addr_t sigevent_addr, addr_t timer_addr) {

if (sigev.method == SIGEV_THREAD_ID_) {
lock(&pids_lock);
if (pid_get_task(sigev.tid) == NULL)
if (pid_get_task(sigev.tid) == NULL) {
unlock(&pids_lock);
return _EINVAL;
}
unlock(&pids_lock);
}

Expand Down