From cbadacbb881200b601c7b53b29aa0c1b78747692 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Tue, 23 Mar 2010 16:34:36 +0100 Subject: More work on IPC --- src/kernel/ipc/object.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/ipc/object.h | 29 ++++++++---- src/kernel/ipc/request.c | 55 +++++++++++++++++++++++ src/kernel/ipc/request.h | 20 ++++++--- src/kernel/ipc/shm.c | 26 +++++++++++ src/kernel/ipc/shm.h | 7 ++- 6 files changed, 234 insertions(+), 16 deletions(-) create mode 100644 src/kernel/ipc/object.c create mode 100644 src/kernel/ipc/request.c (limited to 'src/kernel/ipc') diff --git a/src/kernel/ipc/object.c b/src/kernel/ipc/object.c new file mode 100644 index 0000000..70c31b2 --- /dev/null +++ b/src/kernel/ipc/object.c @@ -0,0 +1,113 @@ +#include "object.h" +#include +#include + +struct object* obj_new(struct process* owner) { + struct object* ret = kmalloc(sizeof(struct object)); + ret->owner = owner; + ret->descriptors = 0; + ret->busyMutex = MUTEX_UNLOCKED; + ret->request = 0; + return ret; +} + +void obj_delete(struct object* obj) { + if (obj->descriptors > 0) return; + if (obj->busyMutex != MUTEX_UNLOCKED) return; + if (obj->request != 0) return; + kfree(obj); +} + +int obj_createP(struct process* p) { + return objdesc_add(p, obj_new(p)); +} + +void obj_closeP(struct process* p, int id) { + struct object* obj = objdesc_read(p, id); + if (obj == 0) return; + objdesc_rm(p, id); + if (obj->owner == p) { + if (obj->descriptors > 0) { + obj->owner = 0; // set object to be invalid + //if a request was being handled, give it a null response + } else { + obj_delete(obj); + } + } else { + if (obj->descriptors == 0 && obj->owner == 0) { + obj_delete(obj); + } + //future : send message becuz object was closed + } +} + +void obj_closeall(struct process* p) { + while (p->objects != 0) obj_closeP(p, p->objects->id); +} + +// DESCRIPTORS + +int objdesc_add(struct process* proc, struct object* obj) { + struct obj_descriptor *ret = kmalloc(sizeof(struct obj_descriptor)); + ret->obj = obj; + ret->id = proc->next_objdesc; + ret->next = proc->objects; + proc->objects = ret; + obj->descriptors++; + proc->next_objdesc++; + return ret->id; +} + +int objdesc_get(struct process* proc, struct object* obj) { + struct obj_descriptor *it = proc->objects; + while (it != 0) { + if (it->obj == obj) return it->id; + it = it->next; + } + return -1; +} + +struct object* objdesc_read(struct process* proc, int id) { + struct obj_descriptor *it = proc->objects; + while (it != 0) { + if (it->id == id) return it->obj; + it = it->next; + } + return 0; +} + +void objdesc_rm(struct process* proc, int id) { + struct obj_descriptor *e = proc->objects; + if (e != 0 && e->id == id) { + proc->objects = e->next; + e->obj->descriptors--; + kfree(e); + return; + } + while (e->next != 0) { + if (e->next->id == id) { + e->next = e->next->next; + e->next->obj->descriptors--; + kfree(e->next); + return; + } + e = e->next; + } +} + +// SYSCALLS + +int object_create() { + return obj_createP(current_thread->process); +} + +int object_owned(int id) { + struct object *obj = objdesc_read(current_thread->process, id); + if (obj == 0) return -1; + if (obj->owner == current_thread->process) return 1; + return 0; +} + +void object_close(int id) { + obj_closeP(current_thread->process, id); +} diff --git a/src/kernel/ipc/object.h b/src/kernel/ipc/object.h index ef90a66..9dd3a1e 100644 --- a/src/kernel/ipc/object.h +++ b/src/kernel/ipc/object.h @@ -3,24 +3,37 @@ #include -#define OS_INACTIVE 0 //No one doing anything on this -#define OS_LISTENING 1 //A thread is waiting for a request on this object -#define OS_REQUESTPENDING 2 //A request is waiting for a thread to handle it -#define OS_BUSY 3 //A thread is currently handling a request - struct object { - struct process *owner; + struct process *owner; //when 0, object is invalid and cannot handle requests int descriptors; - uint32_t busyMutex; + uint32_t busyMutex; //if busy, either a blocking request is being processed, or a sent message is waiting for being recieved struct request *request; }; struct obj_descriptor { struct object *obj; int id; - int owned; struct obj_descriptor *next; }; +//Objects +struct object* obj_new(struct process *owner); +void obj_delete(struct object* obj); + +int obj_createP(struct process* p); +void obj_closeP(struct process* p, int id); +void obj_closeall(struct process* p); + +//Object descriptors +int objdesc_add(struct process* proc, struct object* obj); // add a descriptor +int objdesc_get(struct process* proc, struct object* obj); // look in descriptors for the one corresponding to the object +struct object* objdesc_read(struct process* proc, int id); // get the object correspoinding to the id +void objdesc_rm(struct process* proc, int id); // remove descriptor for an object + +//Syscalls +int object_create(); +int object_owned(int id); //does current process own object ? 1=yes 0=no +void object_close(int id); //closes descriptor to specified object. if we are the owner, make all requests to object fail. + #endif diff --git a/src/kernel/ipc/request.c b/src/kernel/ipc/request.c new file mode 100644 index 0000000..51a0bbc --- /dev/null +++ b/src/kernel/ipc/request.c @@ -0,0 +1,55 @@ +#include "request.h" + +int request_get(int obj, uint32_t ptr, int wait) { + //check if we own the object, if not return -2 + //check if a request is pending. if request is being processed, return -3 + //if not (busymutex unlocked or request==0) && wait, then wait, else return -1 + //if request pending, write it to ptr + // if request is nonblocking and no shm is to be mapped, delete it and unlock objects busymutex + //return 0 +} + +int request_has(int obj) { + //check if we own the object, if not return -2 + //check if a request is pending. + // if being processed, return 2 + // if waiting for ack, return 1 + // if none (busymutex unlocked or request==0), return 0 +} + +void request_answer(int obj, uint32_t answer) { + //check if we own the object, if not return + //if no blocking request is being processed (including non-acknowledged waiting requests), return + //set blocking request to finished, and set its answer + //dereference request from object, unlock objects busymutex +} + +int request_mapShm(int obj, uint32_t pos, int number) { + //check if we own the object, if not return -2 + //if no request is being processes (including non-acknowledged waiting requests), return -3 + //check if the requests has shm in parameter [number], if not return -4 + //map shm to position + //if request is nonblocking and no more shm is to be mapped, delete request and free object busymutex + //return 0 +} + +static struct request *mkrequest(int obj, struct thread *requester, + uint32_t func, uint32_t a, uint32_t b, uint32_t c) { + // get object from descriptor id, if none return 0 + // waitlock object's busy mutex + // if object cannot answer (owner == 0) return 0 + // create request, fill it up, reference it from object + // return request +} + +int request(int obj, uint32_t func, uint32_t a, uint32_t b, uint32_t c, uint32_t answerptr) { + //call mkrequest with parameters (requester thread = current thread) + //if returned value is 0, return -1 + //sleep until request is handled + //write answer to *answerptr, delete request, return 0 +} + +int send_msg(int obj, uint32_t func, uint32_t a, uint32_t b, uint32_t c) { + //call mkrequest with parameters (requester thread = 0) + //if returned value is 0, return -1 else return 0 +} diff --git a/src/kernel/ipc/request.h b/src/kernel/ipc/request.h index bf90c68..4a35dc3 100644 --- a/src/kernel/ipc/request.h +++ b/src/kernel/ipc/request.h @@ -10,13 +10,12 @@ struct request { struct object *obj; - struct thread *requester; - uint32_t func, param1, param2, param3; - struct seg_map *shm_cli, *shm_srv; - int answerType; + struct thread *requester; //0 if nonblocking message + uint32_t func, params[3]; + struct seg_map *shm_srv[3], *shm_cli[3]; union { - int num; - struct object* obj; + int64_t ll; + uint32_t n; } answer; }; @@ -25,5 +24,14 @@ struct user_request { int hasShm; }; +//syscalls +int request_get(int obj, uint32_t ptr, int wait); +int request_has(int obj); +void request_answer(int obj, uint32_t answer); +int request_mapShm(int obj, uint32_t pos, int number); + +int request(int obj, uint32_t func, uint32_t a, uint32_t b, uint32_t c, uint32_t answerptr); +int send_msg(int obj, uint32_t func, uint32_t a, uint32_t b, uint32_t c); + #endif diff --git a/src/kernel/ipc/shm.c b/src/kernel/ipc/shm.c index 0a0d3ee..1cdacfb 100644 --- a/src/kernel/ipc/shm.c +++ b/src/kernel/ipc/shm.c @@ -1,6 +1,7 @@ #include "shm.h" #include #include +#include struct segment* shmseg_make(size_t len, struct process* owner) { struct shmseg *ss = kmalloc(sizeof(struct shmseg)); @@ -54,3 +55,28 @@ void shmseg_delete(struct segment *seg) { } kfree(ss->frames); } + +struct segment_map* shmseg_getByOff(struct process* pr, size_t offset) { + struct segment_map* m = pr->pagedir->mappedSegs; + while (m != 0) { + if (m->start == offset && m->seg->delete == shmseg_delete) return m; + m = m->next; + } + return 0; +} + +// **** **** SHM syscalls **** **** +int shm_create(size_t offset, size_t len) { + if (offset >= 0xE0000000) return -1; + if (len >= 0x10000000) return -1; + if (offset+len >= 0xE0000000) return -1; + seg_map(shmseg_make(len, current_thread->process), current_thread->process->pagedir, offset); + return 0; +} + +int shm_delete(size_t offset) { + struct segment_map *s = shmseg_getByOff(current_thread->process, offset); + if (s == 0) return -1; + seg_unmap(s); + return 0; +} diff --git a/src/kernel/ipc/shm.h b/src/kernel/ipc/shm.h index b409955..895a619 100644 --- a/src/kernel/ipc/shm.h +++ b/src/kernel/ipc/shm.h @@ -16,9 +16,12 @@ void shmseg_unmap(struct segment_map*); void shmseg_delete(struct segment *seg); int shmseg_handleFault(struct segment_map *map, size_t addr, int write); + //find a shared memory segment in current address space by its offset +struct segment_map* shmseg_getByOff(struct process* pr, size_t offset); + //Shared memory syscalls -void shm_create(size_t offset, size_t len); -void shm_delete(size_t offset); +int shm_create(size_t offset, size_t len); +int shm_delete(size_t offset); #endif -- cgit v1.2.3