Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (7 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_mutex.c

    r15 r23  
    11/*
    2  * kern/sys_rwlock.c - interface to access Read-Write locks service
     2 * sys_mutex.c - Access a POSIX mutex.
    33 *
    4  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
    5  * Copyright (c) 2011,2012 UPMC Sorbonne Universites
     4 * Author    Alain Greiner (2016,2017)
    65 *
    7  * This file is part of ALMOS-kernel.
     6 * Copyright (c) UPMC Sorbonne Universites
    87 *
    9  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * This file is part of ALMOS-MKH.
     9 *
     10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    1011 * under the terms of the GNU General Public License as published by
    1112 * the Free Software Foundation; version 2.0 of the License.
    1213 *
    13  * ALMOS-kernel is distributed in the hope that it will be useful, but
     14 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1415 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1516 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1718 *
    1819 * 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 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
    2021 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2122 */
    2223
    23 #include <types.h>
     24#include <hal_types.h>
     25#include <hal_special.h>
    2426#include <errno.h>
    2527#include <thread.h>
    26 #include <kmem.h>
    27 #include <task.h>
     28#include <printk.h>
    2829#include <vmm.h>
    29 #include <kmagics.h>
    30 #include <rwlock.h>
     30#include <syscalls.h>
     31#include <remote_mutex.h>
    3132
    3233
    33 static inline bool_t isBadLock(struct rwlock_s *rwlock)
    34 {
    35         return vmm_check_object(rwlock, struct rwlock_s, RWLOCK_ID);
    36 }
     34/////////////////////////////////
     35int sys_mutex( void     * vaddr,
     36               uint32_t   operation,
     37               uint32_t   attr )
     38{
     39        error_t    error;
     40    paddr_t    paddr;
    3741
    38 int sys_rwlock(struct rwlock_s **rwlock, uint_t operation)
    39 {
    40         kmem_req_t req;
    41         struct rwlock_s *irwlock;
    42         error_t err;
    43  
    44         err = vmm_check_address("usr rwlock ptr",
    45                                 current_task,
    46                                 rwlock,
    47                                 sizeof(struct rwlock_s*));
    48         if(err)
    49                 goto SYS_RWLOCK_END;
     42    thread_t * this = CURRENT_THREAD;
    5043
    51         if((err = cpu_copy_from_uspace(&irwlock, rwlock, sizeof(struct rwlock_s *))))
    52                 goto SYS_RWLOCK_END;
    53  
    54         switch(operation)
    55         {   
    56         case RWLOCK_INIT:   
    57                 req.type  = KMEM_RWLOCK;
    58                 req.size  = sizeof(*irwlock);
    59                 req.flags = AF_USER;
     44    // check vaddr in user vspace
     45        error = vmm_v2p_translate( false , vaddr , &paddr );
     46        if( error )
     47    {
     48        printk("\n[ERROR] in %s : illegal virtual address = %x\n",
     49               __FUNCTION__ , (intptr_t)vaddr );
     50        this->errno = error;
     51        return -1;
     52    }
    6053
    61                 if((irwlock = kmem_alloc(&req)) == NULL)
    62                 {
    63                         err = ENOMEM;
    64                         break;
     54    // execute requested operation
     55        switch( operation )
     56        {   
     57        ////////////////
     58            case MUTEX_INIT:
     59        {
     60            if( attr != 0 )
     61            {
     62                printk("\n[ERROR] in %s : mutex attributes non supported yet\n",
     63                       __FUNCTION__ );
     64                this->errno = error;
     65                return -1;
     66            }
     67   
     68            error = remote_mutex_create( (intptr_t)vaddr );
     69
     70            if( error )
     71            {
     72                printk("\n[ERROR] in %s : cannot create mutex\n",
     73                       __FUNCTION__ );
     74                this->errno = error;
     75                return -1;
     76            }
     77                    break;
    6578                }
    66    
    67                 if((err = rwlock_init(irwlock)))
    68                         break;
    69    
    70                 if((err = cpu_copy_to_uspace(rwlock, &irwlock, sizeof(struct rwlock_s *))))
    71                 {
    72                         req.ptr = irwlock;
    73                         kmem_free(&req);
    74                 }
    75                 break;
     79        ///////////////////
     80            case MUTEX_DESTROY:
     81        {
     82            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
    7683
    77         case RWLOCK_WRLOCK:
    78                 if(isBadLock(irwlock))
    79                         break;
    80    
    81                 return rwlock_wrlock(irwlock);
     84            if( mutex_xp == XPTR_NULL )     // user error
     85            {
     86                printk("\n[ERROR] in %s : mutex %x not registered\n",
     87                       __FUNCTION__ , (intptr_t)vaddr );
     88                this->errno = EINVAL;
     89                return -1;
     90            }
     91            else                          // success
     92            {
     93                remote_mutex_destroy( mutex_xp );
     94            }
     95            break;
     96        }
     97        ////////////////
     98            case MUTEX_LOCK:
     99        {
     100            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
    82101
    83         case RWLOCK_RDLOCK:
    84                 if(isBadLock(irwlock))
    85                         break;
    86    
    87                 return rwlock_rdlock(irwlock);
     102            if( mutex_xp == XPTR_NULL )     // user error
     103            {
     104                printk("\n[ERROR] in %s : mutex %x not registered\n",
     105                       __FUNCTION__ , (intptr_t)vaddr );
     106                this->errno = EINVAL;
     107                return -1;
     108            }
     109            else                          // success
     110            {
     111                remote_mutex_lock( mutex_xp );
     112            }
     113            break;
     114        }
     115        //////////////////
     116            case MUTEX_UNLOCK:
     117        {
     118            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
    88119
    89         case RWLOCK_TRYWRLOCK:
    90                 if(isBadLock(irwlock))
    91                         break;
    92    
    93                 return rwlock_trywrlock(irwlock);
    94 
    95         case RWLOCK_TRYRDLOCK:
    96                 if(isBadLock(irwlock))
    97                         break;
    98    
    99                 return rwlock_tryrdlock(irwlock);
    100 
    101         case RWLOCK_UNLOCK:
    102                 if(isBadLock(irwlock))
    103                         break;
    104    
    105                 if((err = rwlock_unlock(irwlock)))
    106                         break;
    107 
    108         case RWLOCK_DESTROY:
    109                 if(isBadLock(irwlock))
    110                         break;
    111    
    112                 if((err = rwlock_destroy(irwlock)))
    113                         break;
    114 
    115                 req.type = KMEM_RWLOCK;
    116                 req.ptr  = irwlock;
    117                 kmem_free(&req);
    118                 return 0;
    119 
    120         default:
    121                 err = EINVAL;
     120            if( mutex_xp == XPTR_NULL )     // user error
     121            {
     122                printk("\n[ERROR] in %s : mutex %x not registered\n",
     123                       __FUNCTION__ , (intptr_t)vaddr );
     124                this->errno = EINVAL;
     125                return -1;
     126            }
     127            else                          // success
     128            {
     129                remote_mutex_unlock( mutex_xp );
     130            }
     131            break;
     132        }
     133        ////////
     134            default:
     135        {
     136            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
     137                    hal_core_sleep();
     138        }
    122139        }
    123140
    124 SYS_RWLOCK_END:
    125         current_thread->info.errno = err;
    126         return err;
    127 }
     141        return 0;
    128142
    129 KMEM_OBJATTR_INIT(rwlock_kmem_init)
    130 {
    131         attr->type   = KMEM_RWLOCK;
    132         attr->name   = "KCM RWLOCK";
    133         attr->size   = sizeof(struct rwlock_s);
    134         attr->aligne = 0;
    135         attr->min    = CONFIG_RWLOCK_MIN;
    136         attr->max    = CONFIG_RWLOCK_MAX;
    137         attr->ctor   = NULL;
    138         attr->dtor   = NULL;
    139  
    140         return 0;
    141 }
     143}  // end sys_mutex()
     144
Note: See TracChangeset for help on using the changeset viewer.