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
1 change: 0 additions & 1 deletion include/kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ typedef enum {
SYSCALL_OBJECT_STATS,
SYSCALL_OBJECT_SET_TAG,
SYSCALL_OBJECT_GET_TAG,
SYSCALL_OBJECT_SET_BLOCKING,
SYSCALL_OBJECT_MAX,
SYSCALL_SYSTEM_STATS,
SYSCALL_SYSTEM_TIME,
Expand Down
1 change: 0 additions & 1 deletion include/library/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ int syscall_object_close(int fd);
int syscall_object_stats(int fd, struct object_stats *stats );
int syscall_object_set_tag(int fd, char *tag);
int syscall_object_get_tag(int fd, char *buffer, int buffer_size);
int syscall_object_set_blocking(int fd, int b);
int syscall_object_max();

/* Syscalls that query or affect the whole system state. */
Expand Down
2 changes: 1 addition & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include ../Makefile.config

KERNEL_OBJECTS=kernelcore.o main.o console.o page.o keyboard.o mouse.o clock.o interrupt.o kmalloc.o pic.o ata.o cdromfs.o string.o bitmap.o graphics.o font.o syscall_handler.o process.o mutex.o list.o pagetable.o rtc.o kshell.o fs.o hash_set.o diskfs.o serial.o elf.o device.o kobject.o pipe.o bcache.o printf.o is_valid.o
KERNEL_OBJECTS=kernelcore.o main.o console.o page.o keyboard.o mouse.o clock.o interrupt.o kmalloc.o pic.o ata.o cdromfs.o string.o bitmap.o graphics.o font.o syscall_handler.o process.o mutex.o monitor.o list.o pagetable.o rtc.o kshell.o fs.o hash_set.o diskfs.o serial.o elf.o device.o kobject.o pipe.o bcache.o printf.o is_valid.o

basekernel.img: bootblock kernel
cat bootblock kernel /dev/zero | head -c 1474560 > basekernel.img
Expand Down
27 changes: 21 additions & 6 deletions kernel/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See the file LICENSE for details.
#include "clock.h"
#include "ioports.h"
#include "process.h"
#include "monitor.h"

#define CLICKS_PER_SECOND 10

Expand All @@ -21,12 +22,12 @@ See the file LICENSE for details.
static uint32_t clicks = 0;
static uint32_t seconds = 0;

static struct list queue = { 0, 0 };
static struct monitor monitor = MONITOR_INIT_INTERRUPT_SAFE;

static void clock_interrupt(int i, int code)
{
clicks++;
process_wakeup_all(&queue);
monitor_notify_all(&monitor);
if(clicks >= CLICKS_PER_SECOND) {
clicks = 0;
seconds++;
Expand All @@ -35,11 +36,21 @@ static void clock_interrupt(int i, int code)
}
}

clock_t clock_read()
static clock_t clock_read_unlocked()
{
clock_t result;

result.seconds = seconds;
result.millis = 1000 * clicks / CLICKS_PER_SECOND;

return result;
}

clock_t clock_read()
{
monitor_lock(&monitor);
clock_t result = clock_read_unlocked();
monitor_unlock(&monitor);
return result;
}

Expand All @@ -60,12 +71,16 @@ void clock_wait(uint32_t millis)
clock_t start, elapsed;
uint32_t total;

start = clock_read();
monitor_lock(&monitor);

start = clock_read_unlocked();
do {
process_wait(&queue);
elapsed = clock_diff(start, clock_read());
monitor_wait(&monitor);
elapsed = clock_diff(start, clock_read_unlocked());
total = elapsed.millis + elapsed.seconds * 1000;
} while(total < millis);

monitor_unlock(&monitor);
}

void clock_init()
Expand Down
15 changes: 10 additions & 5 deletions kernel/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ See the file LICENSE for details.
#include "ioports.h"
#include "interrupt.h"
#include "kernel/ascii.h"
#include "process.h"
#include "monitor.h"
#include "device.h"
#include "kernelcore.h"

Expand Down Expand Up @@ -46,7 +46,7 @@ static uint8_t buffer[BUFFER_SIZE];
static int keyboard_buffer_read = 0;
static int keyboard_buffer_write = 0;

static struct list queue = { 0, 0 };
static struct monitor monitor = MONITOR_INIT_INTERRUPT_SAFE;

static int shift_mode = 0;
static int alt_mode = 0;
Expand Down Expand Up @@ -143,17 +143,22 @@ static void keyboard_interrupt(int i, int intr_code)
return;
buffer[keyboard_buffer_write] = c;
keyboard_buffer_write = (keyboard_buffer_write + 1) % BUFFER_SIZE;
process_wakeup(&queue);
monitor_notify(&monitor);
}

char keyboard_read( int non_blocking )
{
monitor_lock(&monitor);
while(keyboard_buffer_read == keyboard_buffer_write) {
if(non_blocking) return -1;
process_wait(&queue);
if(non_blocking) {
monitor_unlock(&monitor);
return -1;
}
monitor_wait(&monitor);
}
char c = buffer[keyboard_buffer_read];
keyboard_buffer_read = (keyboard_buffer_read + 1) % BUFFER_SIZE;
monitor_unlock(&monitor);
return c;
}

Expand Down
14 changes: 0 additions & 14 deletions kernel/kobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,10 @@ int kobject_close(struct kobject *kobject)
kfree(kobject->tag);
kfree(kobject);
return 0;
} else if(kobject->refcount>1 ) {
if(kobject->type==KOBJECT_PIPE) {
pipe_flush(kobject->data.pipe);
}
}
return 0;
}

int kobject_set_blocking(struct kobject *kobject, int b)
{
switch (kobject->type) {
case KOBJECT_PIPE:
return pipe_set_blocking(kobject->data.pipe, b);
default:
return 0;
}
}

int kobject_size(struct kobject *kobject, int *dims, int n)
{
switch (kobject->type) {
Expand Down
1 change: 0 additions & 1 deletion kernel/kobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ struct kobject * kobject_copy( struct kobject *ksrc, struct kobject **kdst );
int kobject_remove( struct kobject *kobject, const char *name );
int kobject_close(struct kobject *kobject);

int kobject_set_blocking(struct kobject *kobject, int b);
int kobject_get_type(struct kobject *kobject);
int kobject_set_tag(struct kobject *kobject, char *new_tag);
int kobject_get_tag(struct kobject *kobject, char *buffer, int buffer_size);
Expand Down
49 changes: 49 additions & 0 deletions kernel/monitor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (C) 2015-2019 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file LICENSE for details.
*/

#include "monitor.h"
#include "process.h"
#include "interrupt.h"

void monitor_lock(struct monitor *m)
{
if(m->type==MONITOR_TYPE_INTERRUPT_SAFE) {
interrupt_block();
} else {
mutex_lock(&m->mutex);
}
}

void monitor_unlock(struct monitor *m)
{
if(m->type==MONITOR_TYPE_INTERRUPT_SAFE) {
interrupt_unblock();
} else {
mutex_unlock(&m->mutex);
}
}

void monitor_wait(struct monitor *m)
{
if(m->type==MONITOR_TYPE_INTERRUPT_SAFE) {
process_wait(&m->queue);
} else {
mutex_unlock_and_wait(&m->mutex,&m->queue);
}
monitor_lock(m);
}

void monitor_notify(struct monitor *m)
{
process_wakeup(&m->queue);
}

void monitor_notify_all(struct monitor *m)
{
while(m->queue.head) {
process_wakeup(&m->queue);
}
}
31 changes: 31 additions & 0 deletions kernel/monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright (C) 2015-2019 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file LICENSE for details.
*/

#ifndef MONITOR_H
#define MONITOR_H

#include "mutex.h"
#include "list.h"

struct monitor {
int type;
struct mutex mutex;
struct list queue;
};

#define MONITOR_TYPE_INTERRUPT_SAFE 1
#define MONITOR_TYPE_PROCESS_SAFE 2

#define MONITOR_INIT_INTERRUPT_SAFE {MONITOR_TYPE_INTERRUPT_SAFE,MUTEX_INIT,LIST_INIT}
#define MONITOR_INIT_PROCESS_SAFE {MONITOR_TYPE_PROCESS_SAFE,MUTEX_INIT,LIST_INIT}

void monitor_lock( struct monitor *m );
void monitor_wait( struct monitor *m );
void monitor_notify( struct monitor *m );
void monitor_notify_all( struct monitor *m );
void monitor_unlock( struct monitor *m );

#endif
13 changes: 11 additions & 2 deletions kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void mutex_lock(struct mutex *m)
{
interrupt_block();
while(m->locked) {
process_wait(&m->waitqueue);
process_wait(&m->queue);
interrupt_block();
}
m->locked = 1;
Expand All @@ -23,6 +23,15 @@ void mutex_unlock(struct mutex *m)
{
interrupt_block();
m->locked = 0;
process_wakeup(&m->waitqueue);
process_wakeup(&m->queue);
interrupt_unblock();
}

void mutex_unlock_and_wait( struct mutex *m, struct list *queue )
{
interrupt_block();
m->locked = 0;
process_wakeup(&m->queue);
process_wait(queue);
/* process wait unblocks interrupts on process switch */
}
7 changes: 4 additions & 3 deletions kernel/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ See the file LICENSE for details.

struct mutex {
int locked;
struct list waitqueue;
struct list queue;
};

#define MUTEX_INIT {0,LIST_INIT}

void mutex_lock(struct mutex *m);
void mutex_unlock(struct mutex *m);
void mutex_lock( struct mutex *m );
void mutex_unlock( struct mutex *m );
void mutex_unlock_and_wait( struct mutex *m, struct list *queue );

#endif
Loading