| [1] | 1 | /* |
|---|
| 2 | * kern/sys_barrier.c - barrier service interface for userland |
|---|
| 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 <task.h> |
|---|
| 27 | #include <kmem.h> |
|---|
| 28 | #include <vmm.h> |
|---|
| 29 | #include <kmagics.h> |
|---|
| 30 | #include <barrier.h> |
|---|
| 31 | |
|---|
| 32 | int sys_barrier(struct barrier_s **barrier, uint_t operation, uint_t count) |
|---|
| 33 | { |
|---|
| 34 | kmem_req_t req; |
|---|
| 35 | struct barrier_s *ibarrier; |
|---|
| 36 | error_t err = EINVAL; |
|---|
| 37 | |
|---|
| 38 | if((err = vmm_check_address("usr barrier ptr", |
|---|
| 39 | current_task, |
|---|
| 40 | barrier, |
|---|
| 41 | sizeof(struct barrier_s*)))) |
|---|
| 42 | goto SYS_BARRIER_END; |
|---|
| 43 | |
|---|
| 44 | if((err = cpu_copy_from_uspace(&ibarrier, barrier, sizeof(struct barrier_s *)))) |
|---|
| 45 | goto SYS_BARRIER_END; |
|---|
| 46 | |
|---|
| 47 | switch(operation) |
|---|
| 48 | { |
|---|
| 49 | case BARRIER_INIT_PRIVATE: |
|---|
| 50 | case BARRIER_INIT_SHARED: |
|---|
| 51 | req.type = KMEM_BARRIER; |
|---|
| 52 | req.size = sizeof(*ibarrier); |
|---|
| 53 | req.flags = AF_USER; |
|---|
| 54 | |
|---|
| 55 | if((ibarrier = kmem_alloc(&req)) == NULL) |
|---|
| 56 | { |
|---|
| 57 | err = ENOMEM; |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if((err = barrier_init(ibarrier, count, operation))) |
|---|
| 62 | break; |
|---|
| 63 | |
|---|
| 64 | if((err = cpu_copy_to_uspace(barrier, &ibarrier, sizeof(struct barrier_s *)))) |
|---|
| 65 | { |
|---|
| 66 | req.ptr = ibarrier; |
|---|
| 67 | kmem_free(&req); |
|---|
| 68 | } |
|---|
| 69 | break; |
|---|
| 70 | |
|---|
| 71 | case BARRIER_WAIT: |
|---|
| 72 | |
|---|
| 73 | if((err = vmm_check_object(ibarrier, struct barrier_s, BARRIER_ID))) |
|---|
| 74 | break; |
|---|
| 75 | |
|---|
| 76 | err = barrier_wait(ibarrier); |
|---|
| 77 | break; |
|---|
| 78 | |
|---|
| 79 | case BARRIER_DESTROY: |
|---|
| 80 | |
|---|
| 81 | if((err = vmm_check_object(ibarrier, struct barrier_s, BARRIER_ID))) |
|---|
| 82 | break; |
|---|
| 83 | |
|---|
| 84 | if((err = barrier_destroy(ibarrier))) |
|---|
| 85 | break; |
|---|
| 86 | |
|---|
| 87 | req.type = KMEM_BARRIER; |
|---|
| 88 | req.ptr = ibarrier; |
|---|
| 89 | kmem_free(&req); |
|---|
| 90 | return 0; |
|---|
| 91 | |
|---|
| 92 | default: |
|---|
| 93 | err = EINVAL; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | SYS_BARRIER_END: |
|---|
| 97 | current_thread->info.errno = err; |
|---|
| 98 | return err; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | KMEM_OBJATTR_INIT(barrier_kmem_init) |
|---|
| 102 | { |
|---|
| 103 | attr->type = KMEM_BARRIER; |
|---|
| 104 | attr->name = "KCM Barrier"; |
|---|
| 105 | attr->size = sizeof(struct barrier_s); |
|---|
| 106 | attr->aligne = 0; |
|---|
| 107 | attr->min = CONFIG_BARRIER_MIN; |
|---|
| 108 | attr->max = CONFIG_BARRIER_MAX; |
|---|
| 109 | attr->ctor = NULL; |
|---|
| 110 | attr->dtor = NULL; |
|---|
| 111 | return 0; |
|---|
| 112 | } |
|---|