[1] | 1 | /* |
---|
| 2 | * sys_cond_var: interface to access condition vraibles service |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 6 | * |
---|
| 7 | * This file is part of ALMOS-kernel. |
---|
| 8 | * |
---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 10 | * under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 12 | * |
---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <types.h> |
---|
| 24 | #include <errno.h> |
---|
| 25 | #include <thread.h> |
---|
| 26 | #include <kmem.h> |
---|
| 27 | #include <task.h> |
---|
| 28 | #include <vmm.h> |
---|
| 29 | #include <kmagics.h> |
---|
| 30 | #include <semaphore.h> |
---|
| 31 | #include <cond_var.h> |
---|
| 32 | |
---|
| 33 | static inline bool_t isBadSem(struct semaphore_s *sem) |
---|
| 34 | { |
---|
| 35 | return vmm_check_object(sem, struct semaphore_s, SEMAPHORE_ID); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | static inline bool_t isBadCV(struct cv_s *cv) |
---|
| 39 | { |
---|
| 40 | return vmm_check_object(cv, struct cv_s, COND_VAR_ID); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int sys_cond_var(struct cv_s **cv, uint_t operation, struct semaphore_s **sem) |
---|
| 44 | { |
---|
| 45 | kmem_req_t req; |
---|
| 46 | struct cv_s *icv; |
---|
| 47 | struct semaphore_s *isem; |
---|
| 48 | error_t err = EINVAL; |
---|
| 49 | |
---|
| 50 | if((err = vmm_check_address("usr cv ptr", current_task,cv,sizeof(struct cv_s*)))) |
---|
| 51 | goto SYS_COND_END; |
---|
| 52 | |
---|
| 53 | if((err = cpu_copy_from_uspace(&icv, cv, sizeof(struct cv_s *)))) |
---|
| 54 | goto SYS_COND_END; |
---|
| 55 | |
---|
| 56 | switch(operation) |
---|
| 57 | { |
---|
| 58 | case CV_INIT: |
---|
| 59 | req.type = KMEM_CV; |
---|
| 60 | req.size = sizeof(*icv); |
---|
| 61 | req.flags = AF_USER; |
---|
| 62 | |
---|
| 63 | if((icv = kmem_alloc(&req)) == NULL) |
---|
| 64 | { |
---|
| 65 | err = ENOMEM; |
---|
| 66 | break; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if((err = cv_init(icv))) |
---|
| 70 | break; |
---|
| 71 | |
---|
| 72 | if((err = cpu_copy_to_uspace(cv, &icv, sizeof(struct cv_s *)))) |
---|
| 73 | { |
---|
| 74 | req.ptr = icv; |
---|
| 75 | kmem_free(&req); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | break; |
---|
| 79 | |
---|
| 80 | case CV_WAIT: |
---|
| 81 | err = vmm_check_address("usr sem ptr", |
---|
| 82 | current_task, |
---|
| 83 | sem, sizeof(struct semaphore_s*)); |
---|
| 84 | |
---|
| 85 | if(err) break; |
---|
| 86 | |
---|
| 87 | if((err = cpu_copy_from_uspace(&isem, sem, sizeof(struct semaphore_s *)))) |
---|
| 88 | break; |
---|
| 89 | |
---|
| 90 | if(isBadSem(isem)) |
---|
| 91 | break; |
---|
| 92 | |
---|
| 93 | if(isBadCV(icv)) |
---|
| 94 | break; |
---|
| 95 | |
---|
| 96 | return cv_wait(icv, isem); |
---|
| 97 | |
---|
| 98 | case CV_SIGNAL: |
---|
| 99 | if(isBadCV(icv)) |
---|
| 100 | break; |
---|
| 101 | |
---|
| 102 | return cv_signal(icv); |
---|
| 103 | |
---|
| 104 | case CV_BROADCAST: |
---|
| 105 | if(isBadCV(icv)) |
---|
| 106 | break; |
---|
| 107 | |
---|
| 108 | return cv_broadcast(icv); |
---|
| 109 | |
---|
| 110 | case CV_DESTROY: |
---|
| 111 | if(isBadCV(icv)) |
---|
| 112 | break; |
---|
| 113 | |
---|
| 114 | if((err = cv_destroy(icv))) |
---|
| 115 | break; |
---|
| 116 | |
---|
| 117 | req.type = KMEM_CV; |
---|
| 118 | req.ptr = icv; |
---|
| 119 | kmem_free(&req); |
---|
| 120 | return 0; |
---|
| 121 | |
---|
| 122 | default: |
---|
| 123 | err = EINVAL; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | SYS_COND_END: |
---|
| 127 | current_thread->info.errno = err; |
---|
| 128 | return err; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | KMEM_OBJATTR_INIT(cv_kmem_init) |
---|
| 132 | { |
---|
| 133 | attr->type = KMEM_CV; |
---|
| 134 | attr->name = "KCM Condition Variable"; |
---|
| 135 | attr->size = sizeof(struct cv_s); |
---|
| 136 | attr->aligne = 0; |
---|
| 137 | attr->min = CONFIG_CONDTION_VAR_MIN; |
---|
| 138 | attr->max = CONFIG_CONDTION_VAR_MAX; |
---|
| 139 | attr->ctor = NULL; |
---|
| 140 | attr->dtor = NULL; |
---|
| 141 | |
---|
| 142 | return 0; |
---|
| 143 | } |
---|