2013-09-29 00:21:18 +04:00
|
|
|
/* v/main.c
|
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <gmp.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <uv.h>
|
|
|
|
#include <sigsegv.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <term.h>
|
|
|
|
#include <dirent.h>
|
2014-06-30 22:43:06 +04:00
|
|
|
#include <pmmintrin.h>
|
2014-06-30 20:24:32 +04:00
|
|
|
#include <xmmintrin.h>
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
#define U2_GLOBAL
|
|
|
|
#define C3_GLOBAL
|
|
|
|
#include "all.h"
|
|
|
|
#include "v/vere.h"
|
2014-09-10 20:06:30 +04:00
|
|
|
#include "v/arvo.h"
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
/** Legacy fixed jet linkage. Destroy me please.
|
|
|
|
**/
|
|
|
|
/* External drivers.
|
|
|
|
*/
|
2014-01-07 00:37:42 +04:00
|
|
|
extern u2_ho_driver j2_da(k_164);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2013-12-19 00:42:29 +04:00
|
|
|
/* Built-in battery drivers. Null `cos` terminates.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
u2_ho_driver *HostDriverBase[] = {
|
2014-01-07 00:37:42 +04:00
|
|
|
&j2_k_164_d,
|
2013-09-29 00:21:18 +04:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2014-02-26 00:18:07 +04:00
|
|
|
/* _main_readw(): parse a word from a string.
|
|
|
|
*/
|
|
|
|
static u2_bean
|
|
|
|
_main_readw(const c3_c* str_c, c3_w max_w, c3_w* out_w)
|
|
|
|
{
|
2014-03-08 04:19:38 +04:00
|
|
|
c3_c* end_c;
|
|
|
|
c3_w par_w = strtoul(str_c, &end_c, 0);
|
2014-02-26 00:18:07 +04:00
|
|
|
|
2014-03-08 04:19:38 +04:00
|
|
|
if ( *str_c != '\0' && *end_c == '\0' && par_w < max_w ) {
|
2014-02-26 00:18:07 +04:00
|
|
|
*out_w = par_w;
|
|
|
|
return u2_yes;
|
|
|
|
}
|
|
|
|
else return u2_no;
|
|
|
|
}
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/* _main_getopt(): extract option map from command line.
|
|
|
|
*/
|
|
|
|
static u2_bean
|
|
|
|
_main_getopt(c3_i argc, c3_c** argv)
|
|
|
|
{
|
|
|
|
c3_i ch_i;
|
2014-03-08 04:19:38 +04:00
|
|
|
c3_w arg_w;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
u2_Host.ops_u.abo = u2_no;
|
2014-01-16 05:41:30 +04:00
|
|
|
u2_Host.ops_u.bat = u2_no;
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_Host.ops_u.gab = u2_no;
|
|
|
|
u2_Host.ops_u.loh = u2_no;
|
|
|
|
u2_Host.ops_u.dem = u2_no;
|
2014-03-28 22:38:23 +04:00
|
|
|
u2_Host.ops_u.fog = u2_no;
|
2014-06-04 03:39:28 +04:00
|
|
|
u2_Host.ops_u.fak = u2_no;
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_Host.ops_u.pro = u2_no;
|
|
|
|
u2_Host.ops_u.veb = u2_yes;
|
|
|
|
u2_Host.ops_u.nuu = u2_no;
|
2014-07-30 21:53:30 +04:00
|
|
|
u2_Host.ops_u.mem = u2_no;
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_Host.ops_u.kno_w = DefaultKernel;
|
|
|
|
|
2014-07-30 21:53:30 +04:00
|
|
|
while ( (ch_i = getopt(argc, argv, "I:X:f:k:l:n:p:r:LabcdgqvFM")) != -1 ) {
|
2013-09-29 00:21:18 +04:00
|
|
|
switch ( ch_i ) {
|
2014-07-30 21:53:30 +04:00
|
|
|
case 'M': {
|
|
|
|
u2_Host.ops_u.mem = u2_yes;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'I': {
|
|
|
|
u2_Host.ops_u.imp_c = strdup(optarg);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 05:55:24 +04:00
|
|
|
case 'X': {
|
|
|
|
if ( 0 != strcmp("wtf", optarg) ) {
|
|
|
|
return u2_no;
|
|
|
|
} else u2_Host.ops_u.fog = u2_yes;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'f': {
|
2014-02-26 00:18:07 +04:00
|
|
|
if ( u2_no == _main_readw(optarg, 100, &u2_Host.ops_u.fuz_w) ) {
|
|
|
|
return u2_no;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'k': {
|
2014-02-26 00:18:07 +04:00
|
|
|
if ( u2_no == _main_readw(optarg, 256, &u2_Host.ops_u.kno_w) ) {
|
|
|
|
return u2_no;
|
2014-02-24 23:27:57 +04:00
|
|
|
}
|
2014-02-26 00:18:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'l': {
|
2014-03-08 04:19:38 +04:00
|
|
|
if ( u2_no == _main_readw(optarg, 65536, &arg_w) ) {
|
|
|
|
return u2_no;
|
|
|
|
} else u2_Host.ops_u.rop_s = arg_w;
|
2014-01-17 22:12:42 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-01-17 21:42:47 +04:00
|
|
|
case 'n': {
|
|
|
|
u2_Host.ops_u.nam_c = strdup(optarg);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 21:16:50 +04:00
|
|
|
case 'p': {
|
2014-03-08 04:19:38 +04:00
|
|
|
if ( u2_no == _main_readw(optarg, 65536, &arg_w) ) {
|
|
|
|
return u2_no;
|
|
|
|
} else u2_Host.ops_u.por_s = arg_w;
|
2014-01-15 21:16:50 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-02-26 00:18:07 +04:00
|
|
|
case 'r': {
|
2014-03-08 04:19:38 +04:00
|
|
|
u2_Host.ops_u.raf_c = strdup(optarg);
|
2014-02-26 00:18:07 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'L': { u2_Host.ops_u.loh = u2_yes; break; }
|
2014-06-04 05:33:35 +04:00
|
|
|
case 'F': {
|
|
|
|
u2_Host.ops_u.loh = u2_yes;
|
|
|
|
u2_Host.ops_u.fak = u2_yes;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'a': { u2_Host.ops_u.abo = u2_yes; break; }
|
2014-01-16 05:41:30 +04:00
|
|
|
case 'b': { u2_Host.ops_u.bat = u2_yes; break; }
|
2014-01-15 23:59:08 +04:00
|
|
|
case 'c': { u2_Host.ops_u.nuu = u2_yes; break; }
|
|
|
|
case 'd': { u2_Host.ops_u.dem = u2_yes; break; }
|
|
|
|
case 'g': { u2_Host.ops_u.gab = u2_yes; break; }
|
2013-09-29 00:21:18 +04:00
|
|
|
case 'q': { u2_Host.ops_u.veb = u2_no; break; }
|
|
|
|
case 'v': { u2_Host.ops_u.veb = u2_yes; break; }
|
|
|
|
case '?': default: {
|
|
|
|
return u2_no;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 04:19:38 +04:00
|
|
|
if ( u2_Host.ops_u.rop_s == 0 && u2_Host.ops_u.raf_c != 0 ) {
|
2014-02-26 00:18:07 +04:00
|
|
|
fprintf(stderr, "The -r flag requires -l.\n");
|
|
|
|
return u2_no;
|
|
|
|
}
|
|
|
|
|
2014-01-16 05:41:30 +04:00
|
|
|
if ( u2_yes == u2_Host.ops_u.bat ) {
|
|
|
|
u2_Host.ops_u.dem = u2_yes;
|
|
|
|
u2_Host.ops_u.nuu = u2_yes;
|
|
|
|
}
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-01-17 21:42:47 +04:00
|
|
|
if ( u2_Host.ops_u.nam_c == 0 ) {
|
|
|
|
u2_Host.ops_u.nam_c = getenv("HOSTNAME");
|
|
|
|
if ( u2_Host.ops_u.nam_c == 0 ) {
|
|
|
|
c3_w len_w = sysconf(_SC_HOST_NAME_MAX) + 1;
|
|
|
|
|
2014-04-02 04:47:01 +04:00
|
|
|
u2_Host.ops_u.nam_c = c3_malloc(len_w);
|
2014-01-17 21:42:47 +04:00
|
|
|
if ( 0 != gethostname(u2_Host.ops_u.nam_c, len_w) ) {
|
|
|
|
perror("gethostname");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-16 07:28:22 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
if ( argc != (optind + 1) ) {
|
|
|
|
return u2_no;
|
2013-12-19 00:42:29 +04:00
|
|
|
} else {
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
c3_c* ash_c;
|
|
|
|
|
|
|
|
if ( (ash_c = strrchr(argv[optind], '/')) && (ash_c[1] == 0) ) {
|
|
|
|
*ash_c = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 23:22:07 +04:00
|
|
|
u2_Host.cpu_c = strdup(argv[optind]);
|
2013-09-29 00:21:18 +04:00
|
|
|
return u2_yes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* u2_ve_usage(): print usage and exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
u2_ve_usage(c3_i argc, c3_c** argv)
|
|
|
|
{
|
2014-03-13 00:34:26 +04:00
|
|
|
fprintf(stderr, "%s: usage: [-v] [-k stage] [-p ames_port] computer\n",
|
|
|
|
argv[0]);
|
2013-09-29 00:21:18 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* u2_ve_panic(): panic and exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
u2_ve_panic(c3_i argc, c3_c** argv)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: gross system failure\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* u2_ve_sysopt(): apply option map to system state.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
u2_ve_sysopt()
|
|
|
|
{
|
2014-05-13 23:22:07 +04:00
|
|
|
u2_Local = strdup(u2_Host.cpu_c);
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_System = U2_LIB;
|
|
|
|
u2_Flag_Abort = u2_Host.ops_u.abo;
|
|
|
|
u2_Flag_Garbage = u2_Host.ops_u.gab;
|
|
|
|
u2_Flag_Profile = u2_Host.ops_u.pro;
|
|
|
|
u2_Flag_Verbose = u2_Host.ops_u.veb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf Signal_buf;
|
|
|
|
#ifndef SIGSTKSZ
|
|
|
|
# define SIGSTKSZ 16384
|
|
|
|
#endif
|
|
|
|
static uint8_t Sigstk[SIGSTKSZ];
|
|
|
|
|
|
|
|
volatile enum { sig_none, sig_overflow, sig_interrupt } Sigcause;
|
|
|
|
|
2014-03-05 02:48:59 +04:00
|
|
|
static void _main_cont(void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
|
|
|
(void)(arg1);
|
|
|
|
(void)(arg2);
|
|
|
|
(void)(arg3);
|
|
|
|
siglongjmp(Signal_buf, 1);
|
|
|
|
}
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
static void
|
|
|
|
overflow_handler(int emergency, stackoverflow_context_t scp)
|
|
|
|
{
|
|
|
|
if ( 1 == emergency ) {
|
|
|
|
write(2, "stack emergency\n", strlen("stack emergency" + 2));
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
Sigcause = sig_overflow;
|
2014-03-05 02:48:59 +04:00
|
|
|
sigsegv_leave_handler(_main_cont, NULL, NULL, NULL);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
interrupt_handler(int x)
|
|
|
|
{
|
|
|
|
Sigcause = sig_interrupt;
|
|
|
|
longjmp(Signal_buf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
c3_i
|
|
|
|
main(c3_i argc,
|
|
|
|
c3_c** argv)
|
|
|
|
{
|
|
|
|
c3_w kno_w;
|
|
|
|
|
2014-06-30 20:24:32 +04:00
|
|
|
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
|
2014-06-30 22:43:06 +04:00
|
|
|
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
|
2014-06-30 20:24:32 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
// Parse options.
|
|
|
|
//
|
|
|
|
if ( u2_no == _main_getopt(argc, argv) ) {
|
|
|
|
u2_ve_usage(argc, argv);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-13 23:22:07 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_ve_sysopt();
|
|
|
|
|
2014-06-10 01:56:56 +04:00
|
|
|
printf("~\n");
|
2014-05-13 23:22:07 +04:00
|
|
|
printf("welcome.\n");
|
|
|
|
printf("vere: urbit home is %s\n", u2_Host.cpu_c);
|
|
|
|
printf("vere: hostname is %s\n", u2_Host.ops_u.nam_c);
|
|
|
|
|
2014-01-16 05:41:30 +04:00
|
|
|
if ( u2_yes == u2_Host.ops_u.dem && u2_no == u2_Host.ops_u.bat ) {
|
2013-10-21 22:55:19 +04:00
|
|
|
printf("Starting daemon\n");
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 00:18:07 +04:00
|
|
|
// Seed prng. Don't panic -- just for fuzz testing and election timeouts.
|
2014-03-04 23:17:34 +04:00
|
|
|
//
|
2014-02-26 00:18:07 +04:00
|
|
|
srand(getpid());
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
// Instantiate process globals.
|
|
|
|
{
|
2014-05-13 23:22:07 +04:00
|
|
|
u2_wr_check_init(u2_Host.cpu_c);
|
2014-05-13 02:42:43 +04:00
|
|
|
u2_Host.xit_i = 0;
|
2013-12-19 00:42:29 +04:00
|
|
|
if ( (u2_no == u2_Host.ops_u.nuu) &&
|
|
|
|
(u2_yes == u2_loom_load()) )
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
u2_Host.wir_r = u2_ray_of(0, 0);
|
|
|
|
u2_Wire = u2_Host.wir_r;
|
|
|
|
|
|
|
|
u2_Host.arv_u = u2_Arv;
|
|
|
|
|
|
|
|
u2_Arv->ova.egg_u = u2_Arv->ova.geg_u = 0;
|
|
|
|
|
2013-11-12 11:09:11 +04:00
|
|
|
u2_lo_grab("init", u2_none);
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
// Horrible ancient stuff.
|
|
|
|
//
|
|
|
|
kno_w = u2_Host.arv_u->kno_w;
|
|
|
|
u2_Host.kno_w = kno_w;
|
|
|
|
|
|
|
|
u2_ho_push();
|
2013-12-19 00:42:29 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
else {
|
|
|
|
u2_loom_boot();
|
|
|
|
u2_Host.wir_r = u2_wr_init(c3__rock, u2_ray_of(0, 0), u2_ray_of(1, 0));
|
|
|
|
u2_Wire = u2_Host.wir_r;
|
|
|
|
|
|
|
|
u2_Host.arv_u = u2_Arv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have not loaded from checkpoint, build kernel.
|
|
|
|
//
|
2014-04-08 20:25:49 +04:00
|
|
|
if ( 0 != u2_Host.arv_u->ent_d ) {
|
2013-09-29 00:21:18 +04:00
|
|
|
u2_reck_time(u2_Host.arv_u);
|
|
|
|
u2_reck_numb(u2_Host.arv_u);
|
|
|
|
{
|
|
|
|
c3_c* dyt_c = u2_cr_string(u2_Host.arv_u->wen);
|
|
|
|
|
|
|
|
printf("time: %s\n", dyt_c);
|
|
|
|
free(dyt_c);
|
|
|
|
}
|
2013-12-19 00:42:29 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
else {
|
|
|
|
// Set outside bail trap. Should not be used, but you never know...
|
|
|
|
//
|
|
|
|
if ( 0 != u2_cm_trap() ) {
|
|
|
|
u2_ve_panic(argc, argv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Set boot and goal stages.
|
|
|
|
{
|
|
|
|
if ( (0 == u2_Host.ops_u.kno_w) || (u2_Host.ops_u.kno_w > 255) ) {
|
|
|
|
kno_w = DefaultKernel;
|
|
|
|
} else {
|
|
|
|
kno_w = u2_Host.ops_u.kno_w;
|
2013-12-19 00:42:29 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the system.
|
|
|
|
//
|
|
|
|
{
|
|
|
|
u2_Host.kno_w = u2_Host.ops_u.kno_w;
|
|
|
|
|
|
|
|
u2_reck_boot(u2_Host.arv_u);
|
|
|
|
}
|
|
|
|
u2_cm_done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install signal handlers and set buffers.
|
|
|
|
//
|
|
|
|
// Note that we use the sigmask-restoring variant. Essentially, when
|
|
|
|
// we get a signal, we force the system back into the just-booted state.
|
|
|
|
// If anything goes wrong during boot (above), it's curtains.
|
|
|
|
{
|
2014-03-05 02:48:59 +04:00
|
|
|
if ( 0 != sigsetjmp(Signal_buf, 1) ) {
|
2013-09-29 00:21:18 +04:00
|
|
|
switch ( Sigcause ) {
|
|
|
|
case sig_overflow: printf("[stack overflow]\r\n"); break;
|
|
|
|
case sig_interrupt: printf("[interrupt]\r\n"); break;
|
|
|
|
default: printf("[signal error!]\r\n"); break;
|
|
|
|
}
|
|
|
|
Sigcause = sig_none;
|
|
|
|
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
stackoverflow_deinstall_handler();
|
|
|
|
|
|
|
|
// Print the trace, do a GC, etc.
|
|
|
|
//
|
|
|
|
// This is half-assed at present, so we exit.
|
|
|
|
//
|
|
|
|
u2_lo_sway(0, u2k(u2_wire_tax(u2_Wire)));
|
|
|
|
|
|
|
|
u2_lo_bail(u2_Host.arv_u);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#if 1
|
|
|
|
if ( -1 == stackoverflow_install_handler
|
|
|
|
(overflow_handler, Sigstk, SIGSTKSZ) )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "overflow_handler: install failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
signal(SIGINT, interrupt_handler);
|
|
|
|
signal(SIGIO, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-09 03:37:38 +04:00
|
|
|
u2_lo_grab("main", u2_none);
|
2013-10-19 10:10:25 +04:00
|
|
|
|
2014-03-01 02:52:00 +04:00
|
|
|
u2_lo_loop();
|
2014-01-16 05:41:30 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
return 0;
|
|
|
|
}
|