UbixOS V2  2.0
syscall.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <ubixos/syscalls.h>
30 #include <ubixos/syscall.h>
31 #include <ubixos/sched.h>
32 #include <ubixos/endtask.h>
33 #include <ubixos/spinlock.h>
34 #include <ubixos/vitals.h>
35 #include <sys/trap.h>
36 #include <sys/elf.h>
37 #include <string.h>
38 #include <lib/kprintf.h>
39 #include <ubixos/kpanic.h>
40 /* #include <sde/sde.h> */
41 #include <vmm/vmm.h>
42 
43 void sys_call(struct trapframe *frame) {
44  uint32_t code = 0x0;
45  caddr_t params;
46 
47  struct thread *td = &_current->td;
48 
49  td->frame = frame;
50 
51  int error = 0x0;
52 
53  params = (caddr_t) frame->tf_esp + sizeof(int);
54 
55  code = frame->tf_eax;
56 
57  if (code > totalCalls) {
58  die_if_kernel("Invalid System uCall", frame, frame->tf_eax);
59  kpanic("PID: %i", _current->id);
60  }
61  else if ((uint32_t) systemCalls[code].sc_status == SYSCALL_INVALID) {
62  kprintf("Invalid Call: [%i][0x%X]\n", code, (uint32_t) systemCalls[code].sc_name);
63  frame->tf_eax = -1;
64  frame->tf_edx = 0x0;
65  }
66  else {
67  td->td_retval[0] = 0;
68  td->td_retval[1] = frame->tf_edx;
69 
70  if (systemCalls[code].sc_status == SYSCALL_DUMMY)
71  kprintf("Syscall->abi: [%i], PID: [%i], Code: %i, Call: %s\n", td->abi, _current->id, frame->tf_eax, systemCalls[code].sc_name);
72 /*
73  if (td->abi == ELFOSABI_UBIXOS)
74  error = (int) systemCalls[code].sc_entry( frame->tf_ebx, frame->tf_ecx, frame->tf_edx );
75  else */if (td->abi == ELFOSABI_FREEBSD)
76  error = (int) systemCalls[code].sc_entry(td, params);
77  else
78  error = (int) systemCalls[code].sc_entry(td, params);
79 
80  if (systemCalls[code].sc_status == SYSCALL_DUMMY) {
81  kprintf("DUMMY CALL: (%i)\n", code);
82  return;
83  }
84 
85 //kprintf("ERROR: 0x%X",error);
86  switch (error) {
87  case 0:
88  frame->tf_eax = td->td_retval[0];
89  frame->tf_edx = td->td_retval[1];
90  frame->tf_eflags &= ~PSL_C;
91  break;
92  default:
93  frame->tf_eax = td->td_retval[0];
94  frame->tf_edx = td->td_retval[1];
95  frame->tf_eflags |= PSL_C;
96  break;
97  }
98  }
99 }
100 
101 int invalidCall() {
102  int sys_call;
103 
104  asm(
105  "nop"
106  : "=a" (sys_call)
107  :
108  );
109 
110  kprintf("Invalid System Call #[%i], PID: %i\n", sys_call, _current->id);
111  return (0);
112 }
113 
114 
115 typedef struct _UbixUser UbixUser;
116 struct _UbixUser {
117  char *username;
118  char *password;
119  int uid;
120  int gid;
121  char *home;
122  char *shell;
123 };
124 
126  kprintf("authenticating user %s\n", uu->username);
127 
128  /* MrOlsen 2016-01-01 uh?
129  if(uu->username == "root" && uu->password == "user")
130  {
131  uu->uid = 0;
132  uu->gid = 0;
133  }
134  */
135  uu->uid = -1;
136  uu->gid = -1;
137  return (0);
138 }
139 
140 int sysPasswd(char *passwd) {
141  kprintf("changing user password for user %d\n", _current->uid);
142  return (0);
143 }
144 
146  return (0);
147 }
148 
149 int sysRmModule() {
150  return (0);
151 }
152 
153 int sysGetpid(int *pid) {
154  if (pid)
155  *pid = _current->id;
156  return (0);
157 }
158 
159 int sysExit(int status) {
160  endTask(_current->id);
161  return (0x0);
162 }
163 
164 int sysCheckPid(int pid, int *ptr) {
165  kTask_t *tmpTask = schedFindTask(pid);
166  if ((tmpTask != 0x0) && (ptr != 0x0))
167  *ptr = tmpTask->state;
168  else
169  *ptr = 0x0;
170  return (0);
171 }
172 
173 /************************************************************************
174 
175  Function: int sysGetFreePage();
176  Description: Allocs A Page To The Users VM Space
177  Notes:
178 
179  ************************************************************************/
180 int sysGetFreePage(struct thread *td, uint32_t *count) {
181 
182  td->td_retval[0] = vmm_getFreeVirtualPage(_current->id, *count, VM_THRD);
183  return(0);
184  //return(vmm_getFreeVirtualPage(_current->id, *count, VM_TASK));
185 }
186 
187 int sysGetDrives(uInt32 *ptr) {
188  if (ptr)
189  *ptr = 0x0; //(uInt32)devices;
190  return (0);
191 }
192 
193 int sysGetUptime(uInt32 *ptr) {
194  if (ptr)
195  *ptr = systemVitals->sysTicks;
196  return (0);
197 }
198 
199 int sysGetTime(uInt32 *ptr) {
200  if (ptr)
202  return (0);
203 }
204 
205 int sys_getcwd(struct thread *td, struct sys_getcwd_args *args) {
206  char *buf = (char *) args->buf;
207  char *cwd = _current->oInfo.cwd;
208 
209  while (cwd[0] != '/')
210  cwd++;
211 
212  if (args->buf) {
213  sprintf(buf, "%s", cwd);
214  buf[strlen(cwd)] = '\0';
215 
216  //sprintf(buf, "%s", _current->oInfo.cwd);
217  //buf[strlen(_current->oInfo.cwd)] = '\0';
218  //MrOlsen (2018-01-01) - Why is sprintf not null terminating
219  }
220  // kprintf("GETCWD: [%s][0x%X]\n", _current->oInfo.cwd, args->buf);
221  // kprintf("[%s]", args->buf);
222  return (0);
223 }
224 
225 int sys_sched_yield(struct thread *td, void *args) {
226  sched_yield();
227  return (0);
228 }
229 
230 int sysStartSDE() {
231  int i = 0x0;
232  for (i = 0; i < 1400; i++) {
233  asm("hlt");
234  }
235  //execThread(sdeThread,0x2000),0x0);
236  for (i = 0; i < 1400; i++) {
237  asm("hlt");
238  }
239  return (0);
240 }
taskStruct
Definition: sched.h:62
_UbixUser
Definition: syscall.c:88
syscalls.h
spinlock.h
sysGetTime
void sysGetTime(uInt32 *ptr)
Definition: syscall.c:210
sysGetUptime
void sysGetUptime(uInt32 *ptr)
Definition: syscall.c:204
VM_THRD
#define VM_THRD
Definition: paging.h:51
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
trap.h
sysGetpid
void sysGetpid(int *pid)
Definition: syscall.c:125
sys_getcwd_args
Definition: sysproto_posix.h:132
uu
Definition: kprintf.c:40
string.h
sys_getcwd
int sys_getcwd(struct thread *td, struct sys_getcwd_args *args)
Definition: syscall.c:204
thread
Definition: thread.h:40
vmm.h
PSL_C
#define PSL_C
Definition: cpu.h:37
_UbixUser::gid
int gid
Definition: syscall.c:92
endtask.h
strlen
int strlen(const char *str)
Definition: strlen.c:55
syscall.h
_UbixUser::password
char * password
Definition: syscall.c:90
sysRmModule
void sysRmModule()
Definition: syscall.c:121
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
trapframe::tf_eflags
int tf_eflags
Definition: trap.h:52
_UbixUser::username
char * username
Definition: syscall.c:89
systemCalls
struct syscall_entry systemCalls[]
Definition: syscalls.c:34
taskStruct::td
struct thread td
Definition: sched.h:78
sprintf
int sprintf(char *buf, const char *fmt,...)
Definition: kprintf.c:278
kpanic.h
taskStruct::id
pidType id
Definition: sched.h:63
schedFindTask
kTask_t * schedFindTask(uInt32 id)
Definition: sched.c:207
vmm_getFreeVirtualPage
void * vmm_getFreeVirtualPage(pidType, int, int)
Definition: getfreevirtualpage.c:47
vitalsStruct::timeStart
uint32_t timeStart
Definition: vitals.h:46
syscall_entry::sc_name
char * sc_name
Definition: syscalls.h:53
ELFOSABI_FREEBSD
#define ELFOSABI_FREEBSD
Definition: elf_common.h:164
thread::td_retval
int td_retval[2]
Definition: thread.h:41
systemVitals
vitalsNode * systemVitals
Definition: vitals.c:35
trapframe::tf_eax
int tf_eax
Definition: trap.h:46
invalidCall
void invalidCall(int sys_call)
Definition: syscall.c:238
kprintf.h
_UbixUser::home
char * home
Definition: syscall.c:93
buf
Definition: buf.h:35
vitals.h
vitalsStruct::sysTicks
uint32_t sysTicks
Definition: vitals.h:37
SYSCALL_INVALID
#define SYSCALL_INVALID
Definition: syscalls.h:44
sysGetDrives
void sysGetDrives(uInt32 *ptr)
Definition: syscall.c:198
thread::abi
int abi
Definition: thread.h:48
sys_call
void sys_call(struct trapframe *frame)
Definition: syscall.c:43
uint32_t
__uint32_t uint32_t
Definition: types.h:46
trapframe
Definition: trap.h:34
_UbixUser::shell
char * shell
Definition: syscall.c:94
endTask
void endTask(pidType)
Definition: endtask.c:44
thread::frame
struct trapframe * frame
Definition: thread.h:47
SYSCALL_DUMMY
#define SYSCALL_DUMMY
Definition: syscalls.h:46
caddr_t
char * caddr_t
Definition: types.h:41
_current
kTask_t * _current
Definition: sched.c:50
sysPasswd
void sysPasswd(char *passwd)
Definition: syscall.c:112
taskStruct::uid
uint32_t uid
Definition: sched.h:73
taskStruct::state
tState state
Definition: sched.h:72
sysStartSDE
void sysStartSDE()
Definition: syscall.c:226
die_if_kernel
void die_if_kernel(char *str, struct trapframe *regs, long err)
Definition: trap.c:59
sys_sched_yield
int sys_sched_yield(struct thread *td, void *args)
Definition: syscall.c:224
vitalsStruct::sysUptime
uint32_t sysUptime
Definition: vitals.h:38
trapframe::tf_esp
int tf_esp
Definition: trap.h:54
sysCheckPid
void sysCheckPid(int pid, int *ptr)
Definition: syscall.c:173
sysGetFreePage
void sysGetFreePage(long *ptr, int count, int type)
Definition: syscall.c:188
trapframe::tf_edx
int tf_edx
Definition: trap.h:44
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
totalCalls
int totalCalls
Definition: syscalls.c:92
elf.h
sys_getcwd_args::buf
void * buf
Definition: sysproto_posix.h:134
sysExit
void sysExit(int status)
Definition: syscall.c:169
taskStruct::oInfo
struct osInfo oInfo
Definition: sched.h:69
osInfo::cwd
char cwd[1024]
Definition: sched.h:58
sysAddModule
void sysAddModule()
Definition: syscall.c:117
_UbixUser::uid
int uid
Definition: syscall.c:91
sysAuth
void sysAuth(UbixUser *uu)
Definition: syscall.c:97
sched_yield
void sched_yield()
Definition: sched.c:244