ead4f33d9901c866db962c79b0f4f91d06f013b0
[tpg/acess2.git] / AcessNative / acesskernel_src / main.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * Kernel Main
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #ifdef __LINUX__
12 #include <sys/prctl.h>
13 #endif
14 #include <unistd.h>
15 #include <string.h>
16 #include "../../KernelLand/Kernel/include/logdebug.h"
17
18 #define VALGRIND_CLIENT 0
19
20 // === IMPORTS ===
21 extern int      UI_Initialise(int Width, int Height);
22 extern void     UI_MainLoop(void);
23 extern int      VFS_Init(void);
24 extern int      PTY_Install(char **Arguments);
25 extern int      Video_Install(char **Arguments);
26 extern int      NativeKeyboard_Install(char **Arguments);
27 extern int      NativeFS_Install(char **Arguments);
28 extern void     Debug_SetKTerminal(char *Path);
29 extern int      VT_Install(char **Arguments);
30 extern int      Mouse_Install(char **Arguments);
31 extern int      IPCPipe_Install(char **Arguments);
32 extern int      Net_Install(char **Arguments);
33 extern int      VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
34 extern int      VFS_MkDir(const char *Path);
35 extern int      SyscallServer(void);
36 extern int      Server_Shutdown(void);
37 extern const char       gsKernelVersion[];
38 extern const char       gsGitHash[];
39 extern int      giBuildNumber;
40
41 // === GLOBALS ===
42 const char      *gsAcessDir = "../Usermode/Output/native";
43
44 // === CODE ===
45 #ifndef __WIN32__
46 #define P_NOWAIT        0
47 int spawnv(int flags, const char *execuable, char * const argv[])
48 {
49         int pid = fork();
50         if( pid != 0 )  return pid;
51
52         execv(execuable, argv);
53         perror("spawnv - execve");
54         for(;;);
55 }
56 #endif
57
58 int main(int argc, char *argv[])
59 {
60         char    **rootapp = NULL;
61          int    rootapp_argc, i;
62         // Parse command line settings
63         for( i = 1; i < argc; i ++ )
64         {
65                 if( strcmp(argv[i], "--distroot") == 0 ) {
66                         gsAcessDir = argv[++i];
67                 }
68                 else if( strcmp(argv[i], "--rootapp") == 0 ) {
69                         rootapp = &argv[++i];
70                         rootapp_argc = argc - i;
71                         break;
72                 }
73                 else {
74                         fprintf(stderr, "Unknown command line option '%s'\n", argv[i]);
75                         return -1;
76                 }
77         }       
78
79         // Kernel build information
80         printf("Acess2 Native v%s\n", gsKernelVersion);
81         printf(" Build %i, Git Hash %s\n", giBuildNumber, gsGitHash);
82
83         // Start UI subsystem
84         UI_Initialise(800, 480);
85         
86         // - Ignore SIGUSR1 (used to wake threads)
87         #ifdef SIGUSR1
88         signal(SIGUSR1, SIG_IGN);
89         #endif
90                 
91         // Initialise VFS
92         VFS_Init();
93         // - Start IO Drivers
94         if( Video_Install(NULL) ) {
95                 Log_Error("Init", "Unable to load NativeVideo");
96         }
97         if( NativeKeyboard_Install(NULL) ) {
98                 Log_Error("Init", "Unable to load NativeKeyboard");
99         }
100         NativeFS_Install(NULL);
101         Mouse_Install(NULL);
102         IPCPipe_Install(NULL);
103         PTY_Install(NULL);
104         Net_Install(NULL);
105         // - Start VTerm
106         {
107                 char    *args[] = {
108                         "Video=NativeVideo",
109                         "Input=NativeKeyboard",
110                         NULL
111                         };
112                 VT_Install(args);
113         }
114
115         VFS_MkDir("/Acess");    
116         VFS_Mount(gsAcessDir, "/Acess", "nativefs", "");
117
118         Debug_SetKTerminal("/Devices/pts/vt7c");
119         
120         // Start syscall server
121         SyscallServer();
122         
123         // Spawn root application
124         if( rootapp )
125         {
126                  int    pid;
127                  int    argcount = 0;
128                 const char      *args[7+rootapp_argc+1+1];
129
130                 #if VALGRIND_CLIENT
131                 args[argcount++] = "valgrind";
132                 #endif
133                 args[argcount++] = "./ld-acess";
134                 args[argcount++] = "--open";    args[argcount++] = "/Devices/pts/vt0";
135                 args[argcount++] = "--open";    args[argcount++] = "/Devices/pts/vt0";
136                 args[argcount++] = "--open";    args[argcount++] = "/Devices/pts/vt0";
137                 for( i = 0; i < rootapp_argc; i ++ )
138                         args[argcount+i] = rootapp[i];
139                 args[argcount+rootapp_argc] = NULL;
140                 pid = spawnv(P_NOWAIT, "./ld-acess", (char *const*)args);
141                 if(pid < 0) {
142                         perror("Starting root application [fork(2)]");
143                         return 1;
144                 }
145                 printf("Root application running as PID %i\n", pid);
146         }
147
148         UI_MainLoop();
149
150         return 0;
151 }
152
153 void AcessNative_Exit(void)
154 {
155         // TODO: Close client applications too
156         Server_Shutdown();
157         exit(0);
158 }
159
160 uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem)
161 {
162         if(Rem) *Rem = Num % Den;
163         return Num / Den;
164 }
165
166 int Module_EnsureLoaded(const char *Name)
167 {
168         return 0;
169 }
170
171 void Proc_PrintBacktrace(void)
172 {
173         
174 }
175

UCC git Repository :: git.ucc.asn.au