/*
 * do_syscall.c - architecture independant entry-point for system calls.
 * 
 * Author    Alain Greiner (2016)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <hal_types.h>
#include <hal_irqmask.h>
#include <do_syscall.h>
#include <errno.h>
#include <thread.h>
#include <printk.h>
#include <syscalls.h>
#include <shared_syscalls.h>

///////////////////////////////////////////////////////////////////////////////////////
// This ƒonction should never be called...
///////////////////////////////////////////////////////////////////////////////////////
static int sys_undefined()
{
    panic("undefined system call");
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////
// This array of pointers define the kernel functions implementing the syscalls.
// It must be kept consistent with the enum in "shared_syscalls.h" file.
///////////////////////////////////////////////////////////////////////////////////////

typedef int (*sys_func_t) ();

static const sys_func_t syscall_tbl[SYSCALLS_NR] = 
{
    sys_thread_exit,        // 0
    sys_thread_yield,       // 1 
    sys_thread_create,      // 2
    sys_thread_join,        // 3
    sys_thread_detach,      // 4
    sys_undefined,          // 5
    sys_sem,                // 6
    sys_condvar,            // 7
    sys_barrier,            // 8
    sys_mutex,              // 9

    sys_exit,               // 10
    sys_munmap,             // 11
    sys_open,               // 12
    sys_mmap,               // 13
    sys_read,               // 14
    sys_write,              // 15
    sys_lseek,              // 16
    sys_close,              // 17
    sys_unlink,             // 18
    sys_pipe,               // 19

    sys_chdir,              // 20
    sys_mkdir,              // 21
    sys_mkfifo,             // 22
    sys_opendir,            // 23
    sys_readdir,            // 24
    sys_closedir,           // 25
    sys_getcwd,             // 26
    sys_undefined,          // 27  
    sys_alarm,              // 28
    sys_rmdir,              // 29

    sys_utls,               // 30
    sys_chmod,              // 31
    sys_signal,             // 32 
    sys_timeofday,          // 33
    sys_kill,               // 34
    sys_getpid,             // 35
    sys_fork,               // 36
    sys_exec,               // 37
    sys_stat,               // 38
    sys_trace,              // 39

    sys_get_config,         // 40
    sys_get_core,           // 41
    sys_get_cycle,          // 42
    sys_get_sched,          // 43
    sys_panic,              // 44
    sys_thread_sleep,       // 45
    sys_thread_wakeup,      // 46
};

////////////////////////////////////
char * syscall_str( uint32_t index )
{
	if     ( index == SYS_THREAD_EXIT    ) return "THREAD_EXIT";      // 0
	else if( index == SYS_THREAD_YIELD   ) return "THREAD_YIELD";     // 1
	else if( index == SYS_THREAD_CREATE  ) return "THREAD_CREATE";    // 2
	else if( index == SYS_THREAD_JOIN    ) return "THREAD_JOIN";      // 3
	else if( index == SYS_THREAD_DETACH  ) return "THREAD_DETACH";    // 4
	else if( index == SYS_SEM            ) return "SEM";              // 6
	else if( index == SYS_CONDVAR        ) return "CONDVAR";          // 7
	else if( index == SYS_BARRIER        ) return "BARRIER";          // 8
	else if( index == SYS_MUTEX          ) return "MUTEX";            // 9

    else if( index == SYS_EXIT           ) return "EXIT";             // 10
    else if( index == SYS_MUNMAP         ) return "MUNMAP";           // 11
	else if( index == SYS_OPEN           ) return "OPEN";             // 12
	else if( index == SYS_MMAP           ) return "MMAP";             // 13
	else if( index == SYS_READ           ) return "READ";             // 14
	else if( index == SYS_WRITE          ) return "WRITE";            // 15
	else if( index == SYS_LSEEK          ) return "LSEEK";            // 16
	else if( index == SYS_CLOSE          ) return "CLOSE";            // 17
	else if( index == SYS_UNLINK         ) return "UNLINK";           // 18
	else if( index == SYS_PIPE           ) return "PIPE";             // 19

	else if( index == SYS_CHDIR          ) return "CHDIR";            // 20
	else if( index == SYS_MKDIR          ) return "MKDIR";            // 21
	else if( index == SYS_MKFIFO         ) return "MKFIFO";           // 22    
	else if( index == SYS_OPENDIR        ) return "OPENDIR";          // 23
	else if( index == SYS_READDIR        ) return "READDIR";          // 24
	else if( index == SYS_CLOSEDIR       ) return "CLOSEDIR";         // 25
	else if( index == SYS_GETCWD         ) return "GETCWD";           // 26
	else if( index == SYS_ALARM          ) return "ALARM";            // 28
	else if( index == SYS_RMDIR          ) return "RMDIR";            // 29

	else if( index == SYS_UTLS           ) return "UTLS";             // 30
	else if( index == SYS_CHMOD          ) return "CHMOD";            // 31
	else if( index == SYS_SIGNAL         ) return "SIGNAL";           // 32
	else if( index == SYS_TIMEOFDAY      ) return "TIMEOFDAY";        // 33
	else if( index == SYS_KILL           ) return "KILL";             // 34
	else if( index == SYS_GETPID         ) return "GETPID";           // 35
	else if( index == SYS_FORK           ) return "FORK";             // 36
	else if( index == SYS_EXEC           ) return "EXEC";             // 37
	else if( index == SYS_STAT           ) return "STAT";             // 38
	else if( index == SYS_TRACE          ) return "TRACE";            // 39

    else if( index == SYS_GET_CONFIG     ) return "GET_CONFIG";       // 40
    else if( index == SYS_GET_CORE       ) return "GET_CORE";         // 41
    else if( index == SYS_GET_CYCLE      ) return "GET_CYCLE";        // 42
    else if( index == SYS_GET_SCHED      ) return "GET_SCHED";        // 43
    else if( index == SYS_PANIC          ) return "PANIC";            // 44
	else if( index == SYS_SLEEP          ) return "SLEEP";            // 45
	else if( index == SYS_WAKEUP         ) return "WAKEUP";           // 46

    else                                   return "undefined";    
}


//////////////////////////////////
reg_t do_syscall( thread_t * this,
                  reg_t      arg0,
		          reg_t      arg1,
		          reg_t      arg2,
		          reg_t      arg3,
		          reg_t      service_num )
{
	int  error = 0;
	
    // update user time 
	thread_user_time_update( this );

    // check syscall index
	if( service_num >= SYSCALLS_NR )
	{
		printk("\n[ERROR] in %s : Undefined syscall %d, for thread %x\n",
		       __FUNCTION__ , service_num , this );

		this->errno = ENOSYS;
	    hal_disable_irq(NULL);
		return ENOSYS;;
	}

    // reset errno
	this->errno = 0;

    // call relevant kernel function
	error = syscall_tbl[service_num] ( arg0 , arg1 , arg2 , arg3 );

    // update kernel time
	thread_kernel_time_update( this );

	return error;
}
