1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include "usermode.h"
#define FOOLOS_MODULE_NAME "usermode"
#include "lib/logger/log.h"
#include "syscalls.h"
tss_struct sys_tss; //Define the TSS as a global structure
extern uint32_t stack_top[];
// generic syscall interface!
int syscall(int call, int p1, int p2, int p3)
{
int ebx; // will hold return value;
asm("pusha");
// select syscall
asm("mov %0, %%eax"::"m"(call));
// pass params
asm("mov %0,%%edx"::"m"(p1));
asm("mov %0,%%ecx"::"m"(p2));
asm("mov %0,%%ebx"::"m"(p3));
// interrrupt
asm("int $0x80");
// get return value
asm("mov %%ebx, %0": "=b" (ebx));
asm("popa");
return ebx;
}
int write(int file, char *ptr, int len)
{
return syscall(SYSCALL_WRITE,file,ptr,len);
}
int execve(char *name, char **argv, char **env)
{
return syscall(SYSCALL_EXECVE,name,argv,env);
}
void install_tss(int cpu_no){
// now fill each value
// set values necessary
sys_tss.ss0 = 0x10; //kernel data
sys_tss.esp0 = stack_top; //kernel stack just under the kernel
// now set the IO bitmap (not necessary, so set above limit)
// sys_tss.iomap = ( unsigned short ) sizeof( tss_struct );
}
void switch_to_user_mode()
{
char text[]="[internal] ";
//asm_usermode();
write(1,text,11);
while(1); // will not be reached?
}
char *argv_init[]={"/bin/init",NULL};
char *env_init[]={NULL};
// THIS WILL BE RUN IN RING 3!
void userfunc()
{
execve("/bin/init",argv_init,env_init);
for(int i=0;i<3;i++)
{
log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"we are usermode!");
}
char text[]="syscalling!";
write(1,text,10);
while(1)
{
}
}
|