Stashing.

This commit is contained in:
C. Guy Yarvin 2014-09-22 09:10:57 -07:00
parent d426dba971
commit 6ffa794e8f
14 changed files with 522 additions and 71 deletions

View File

@ -303,6 +303,7 @@ LIBED25519=outside/ed25519/ed25519.a
LIBANACHRONISM=outside/anachronism/build/libanachronism.a
vere: $(BIN)/vere
meme: $(BIN)/meme
all: vere
@ -331,6 +332,10 @@ $(BIN)/vere: $(LIBCRE) $(VERE_OFILES) $(LIBUV) $(LIBRE2) $(LIBED25519) $(LIBANAC
mkdir -p $(BIN)
$(CLD) $(CLDOSFLAGS) -o $(BIN)/vere $(VERE_OFILES) $(LIBUV) $(LIBCRE) $(LIBRE2) $(LIBED25519) $(LIBANACHRONISM) $(LIBS)
$(BIN)/meme: $(LIBCRE) $(VERE_OFILES) $(LIBUV) $(LIBRE2) $(LIBED25519) $(LIBANACHRONISM)
mkdir -p $(BIN)
$(CLD) $(CLDOSFLAGS) -o $(BIN)/meme $(MEME_OFILES) $(LIBUV) $(LIBCRE) $(LIBRE2) $(LIBED25519) $(LIBANACHRONISM) $(LIBS)
tags:
ctags -R -f .tags --exclude=root

394
g/e.c Normal file
View File

@ -0,0 +1,394 @@
/* g/m.c
**
** This file is in the public domain.
*/
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include "all.h"
/* u3_ce_fault(): handle a memory event with libsigsegv protocol.
*/
static c3_i
u3_ce_fault(void* adr_v, c3_i ser_i)
{
if ( ser_i ) {
c3_w* adr_w = (c3_w*) adr_v;
if ( (adr_w < u3_Loom) || (adr_w > (u3_Loom + u3_cc_size)) ) {
fprintf(stderr, "address %p out of loom!\r\n", adr_v);
return 0;
}
else {
c3_w off_w = (adr_w - u3_Loom);
c3_w pag_w = off_w >> u3_cc_page;
c3_w blk_w = (pag_w >> 5);
c3_w bit_w = (pag_w & 31);
c3_assert(0 == (u3P.dit_w[blk_w] & bit_w));
u3P.dit_w[blk_w] |= (1 << bit_w);
if ( -1 == mprotect((void *)(u3_Loom + (pag_w << u3_cc_page)),
(1 << (u3_cc_page + 2)),
(PROT_READ | PROT_WRITE)) )
{
perror("mprotect");
return 0;
}
}
}
return 1;
}
/* u3_ce_sync(): write a checkpoint at the current state.
*/
for ( ceg_u = &LoomSegmentA; ceg_u; ceg_u = ceg_u->nex_u ) {
if ( (pag_w >= ceg_u->bot_w) &&
(win_w=(pag_w - ceg_u->bot_w)) < ceg_u->len_w )
{
if ( win_w >= ceg_u->pgs_w ) {
ceg_u->pgs_w = win_w + 1;
}
break;
}
}
if ( 0 == ceg_u ) {
fprintf(stderr, "page %d is not in a segment!\n", pag_w);
return 1;
}
return 1;
}
return 0;
}
/* _ce_image_open(): open or create image. yes if it already exists.
*/
static c3_o
_ce_image_open(u3_cs_image* img_u)
{
c3_c ful_c[8193];
c3_i fid_i;
snprintf(ful_c, 8192, "%s", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk/%s.bin", u3P.cpu_c, img_u->nam_c);
if ( -1 != (img_u->fid_i = open(ful_c, O_RDWR)) ) {
struct stat buf_u;
if ( -1 == fstat(fid_i, &buf_u) )
perror(ful_c);
c3_assert(0);
return u3_no;
}
else {
c3_d siz_d = buf_u.st_siz;
c3_d pgs_d = (siz_d + (c3_d)((1 << (u3_cc_page + 2)) - 1)) >>
(c3_d)(u3_cc_page + 2);
if ( siz_d != (pgs_d << (c3_d)(u3_cc_page + 2)) ) {
fprintf(stderr, "%s: corrupt size %llx\r\n", ful_c, siz_d);
c3_assert(0);
return u3_no;
}
img_u->pgs_w = (c3_w) pgs_d;
c3_assert(pgs_d == (c3_d)img_u->pgs_w);
return u3_yes;
}
}
else {
if ( -1 == (img_u->fid_i = open(ful_c, O_RDWR | O_CREAT)) ) {
perror(ful_c);
c3_assert(0);
}
else {
img_u->pgs_w = 0;
return u3_no;
}
}
}
/* u3_ce_boot(): start the memory system.
*/
void
u3_ce_boot(c3_c* cpu_c)
{
/* Map at fixed address.
*/
{
void* map_v;
map_v = mmap((void *)u3_Loom,
(u3_cc_size << 2),
PROT_READ,
(MAP_ANON | MAP_FIXED | MAP_PRIVATE),
-1, 0);
if ( -1 == (c3_ps)map_v ) {
map_v = mmap((void *)0,
(u3_cc_size << 2),
PROT_READ,
MAP_ANON | MAP_PRIVATE,
-1, 0);
if ( -1 == (c3_ps)map_v ) {
fprintf(stderr, "map failed twice\n");
} else {
fprintf(stderr, "map failed - try U2_OS_LoomBase %p\n", map_v);
}
exit(1);
}
printf("loom: mapped %dMB\n", (u3_cc_size >> 18));
}
/* Open and load, or create, image files.
*/
{
u3P.cpu_c = cpu_c;
u3P.nor_u.nam_c = "north";
u3P.sou_u.nam_c = "south";
if ( u3_yes == _ce_image_open(&u3P.nor_u) ) {
_ce_image_blit_north(&u3P.nor_u);
}
if ( u3_yes == _ce_image_open(&u3P.sou_u) ) {
_ce_image_blit_south(&u3P.sou_u);
}
}
/* Open and apply any patches.
*/
{
u3_cs_patch* pat_u;
if ( 0 != (pat_u = _ce_patch_read()) ) {
_ce_patch_memory(pat_u);
_ce_image_patch(pat_u, &u3P.nor_u, &u3P.sou_u);
_ce_image_fsync(&u3P.nor_u);
_ce_image_fsync(&u3P.sou_u);
_ce_patch_delete();
}
}
}
/* _ce_patch_write_control(): write control block file.
*/
static void
_ce_patch_write_control(u3_cs_patch* pat_u)
{
c3_w len_w = sizeof(u3_cs_control) +
(pat_u->con_u->pgs_w * sizeof(u3_cs_line));
if ( len_w != write(pat_u->ctl_i, pat_u->con_u, len_w) ) {
c3_assert(0);
}
}
/* _ce_patch_read_control(): read control block file.
*/
static void
_ce_patch_read_control(u3_cs_patch* pat_u)
{
u3_cs_control* con_u;
c3_w len_w;
c3_assert(0 == pat_u->con_u);
{
struct stat buf_u;
if ( -1 == fstat(pat_u->ctl_i, &buf_u) ) {
c3_assert(0);
return u3_no;
}
len_w = (c3_w) buf_u.st_size;
}
pat_u->con_u = malloc(len_w);
if ( (len_w != read(pat_u->ctl_i, pat_u->con_u, len_w)) ||
(len_w != sizeof(u3_cs_control) +
(pat_u->con_u->pgs_w * sizeof(u3_cs_line))) )
{
free(pat_u->con_u);
pat_u->con_u = 0;
return u3_no;
}
}
/* _ce_patch_create(): create patch files.
*/
static c3_i
_ce_patch_create(u3_cs_patch* pat_u)
{
c3_i fid_i;
snprintf(ful_c, 8192, "%s", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/control.bin", u3P.cpu_c);
if ( -1 == (pat_u->ctl_i = open(ful_c, O_WRONLY | O_CREAT | O_EXCL, 0666)) ) {
c3_assert(0);
return 0;
}
snprintf(ful_c, 8192, "%s/.urb/memory.bin", u3P.cpu_c);
if ( -1 == (pat_u->mem_i = open(ful_c, O_WRONLY | O_CREAT | O_EXCL, 0666)) ) {
c3_assert(0);
return 0;
}
}
/* _ce_patch_open(): open patch, if any.
*/
static u3_cs_patch*
_ce_patch_open(void)
{
u3_cs_patch* pat_u;
c3_i ctl_i, mem_i;
snprintf(ful_c, 8192, "%s", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.cpu_c);
mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/control.bin", u3P.cpu_c);
if ( -1 == (ctl_i = open(ful_c, O_RDONLY)) ) {
return 0;
}
snprintf(ful_c, 8192, "%s/.urb/memory.bin", u3P.cpu_c);
if ( -1 == (mem_i = open(ful_c, O_RDONLY)) ) {
close(ctl_i);
return 0;
}
pat_u = malloc(sizeof(u3_cs_patch));
pat_u->ctl_i = ctl_i;
pat_u->mem_i = mem_i;
pat_u->con_u = 0;
if ( u3_no == _ce_patch_read_control(pat_u) ) {
close(pat_u->ctl_i);
close(pat_u->mem_i);
free(pat_u);
return 0;
}
return pat_u;
}
/* _ce_patch_compose(): make current patch.
*/
static u3_cs_patch*
_ce_patch_compose(void)
{
c3_w pgs_w = 0;
c3_w nor_w = 0;
c3_w sou_w = 0;
/* Calculate number of saved pages, north and south.
*/
{
c3_w nwr_w, swu_w;
u3_cm_water(&nwr_w, &swu_w);
nor_w = (nwr_w + ((1 << u3_cc_page) - 1)) >> u3_cc_page;
sou_w = (swu_w + ((1 << u3_cc_page) - 1)) >> u3_cc_page;
}
/* Count dirty pages in northward (low) section.
*/
{
c3_w i_w;
for ( i_w = 0; i_w < nor_w; i++ ) {
c3_w blk_w = (i_w >> 5);
c3_w bit_w = (i_w & 31);
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
pgs_w += 1;
}
}
}
/* Count dirty pages in southward (high) section.
*/
{
c3_w i_w;
for ( i_w = 0; i_w < sou_w; i++ ) {
c3_w j_w = (u3_cc_pages - (i_w + 1));
c3_w blk_w = (j_w >> 5);
c3_w bit_w = (j_w & 31);
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
pgs_w += 1;
}
}
}
if ( !pgs_w ) {
return 0;
}
else {
u3_cs_patch* pat_u = malloc(sizeof u3_cs_patch);
c3_w i_w, pgc_w;
_ce_patch_create(pat_u);
/* Build and fill control block.
*/
{
pat_u->con_u = malloc(sizeof(u3_cs_control) +
(pgs_w + sizeof(u3_cs_line)));
pgc_w = 0;
for ( i_w = 0; i_w < nor_w; i_w++ ) {
c3_w blk_w = (i_w >> 5);
c3_w bit_w = (i_w & 31);
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
pgs_w += 1;
}
}
c3_assert(0 == (u3P.dit_w[blk_w] & bit_w));
u3P.dit_w[blk_w] |= (1 << bit_w);
/* Handle intermediate section.
*/
/*
pgs_w = 0;
for ( i_w = 0; i_w < (u3_cc_pages >> 5); i_w++ ) {
}
}
/* u3_ce_save(): save current changes.
*/
void
u3_ce_save(void)
{
u3_cs_patch* pat_u = _ce_patch_compose();
_ce_patch_save(pat_u);
}

82
g/m.c
View File

@ -41,8 +41,36 @@ u3_cm_file(c3_c* pas_c)
}
}
/* _boot_north(): install a north road.
/* _find_north(): point to a north home.
*/
static u3_road*
_find_north(c3_w* mem_w, c3_w siz_w, c3_w len_w)
{
c3_w* rut_w = mem_w;
c3_w* hat_w = rut_w;
c3_w* mat_w = ((mem_w + len_w) - siz_w);
c3_w* cap_w = mat_w;
u3_road* rod_u = (void*) mat_w;
return rod_u;
}
#if 0
/* _find_south(): install a south road.
*/
static u3_road*
_find_south(c3_w* mem_w, c3_w siz_w, c3_w len_w)
{
c3_w* rut_w = mem_w;
c3_w* hat_w = rut_w;
c3_w* mat_w = ((mem_w + len_w) - siz_w);
c3_w* cap_w = mat_w;
u3_road* rod_u = (void*) mat_w;
return rod_u;
}
#endif
static u3_road*
_boot_north(c3_w* mem_w, c3_w siz_w, c3_w len_w)
{
@ -94,36 +122,36 @@ _boot_parts(void)
u3R->jed.har_u = u3_ch_new();
}
/* u3_cm_load(): load u3H in existing
*/
void
u3_cm_boot(void)
{
if ( u3_so(new_o) ) {
u3H = (u3_cs_home *)_boot_north(u3_Loom, c3_wiseof(u3_cs_home), len_w);
}
else {
u3R = &u3H->rod_u;
_boot_parts();
}
/* u3_cm_load(): make u3R and u3H from nothing.
*/
void
u3_cm_load(void)
{
u3H = (u3_cs_home *)_find_north(u3_Loom, c3_wiseof(u3_cs_home), len_w);
u3R = &u3H->rod_u;
}
/* u3_cm_boot(): make u3R and u3H from nothing.
*/
void
u3_cm_boot(c3_p adr_p, c3_w len_w)
u3_cm_boot(void)
{
void* map_v;
map_v = mmap((void *)(c3_p)adr_p,
(len_w << 2),
PROT_READ | PROT_WRITE,
(MAP_ANON | MAP_FIXED | MAP_PRIVATE),
-1, 0);
if ( -1 == (c3_ps)map_v ) {
map_v = mmap((void *)0,
(len_w << 2),
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE,
-1, 0);
if ( -1 == (c3_ps)map_v ) {
fprintf(stderr, "map failed twice\n");
} else {
fprintf(stderr, "map failed - try U2_OS_LoomBase %p\n", map_v);
}
exit(1);
}
printf("loom: mapped %dMB\n", (len_w >> 18));
u3_Loom = map_v;
u3H = (u3_cs_home *)_boot_north(map_v, c3_wiseof(u3_cs_home), len_w);
u3H = (u3_cs_home *)_boot_north(u3_Loom, c3_wiseof(u3_cs_home), len_w);
u3R = &u3H->rod_u;
_boot_parts();

View File

@ -4,25 +4,26 @@
*/
/** Prefix definitions:
***
*** u2_ca_: fundamental allocators.
*** u2_cc_: constants.
*** u2_ch_: HAMT hash tables.
*** u2_ci_: noun constructors
*** u2_cj_: jets.
*** u2_ck*: direct jet calls (modern C convention)
*** u2_cm_: system management etc.
*** u2_cn_: nock interpreter.
*** u2_co_: fundamental macros.
*** u2_cq*: direct jet calls (archaic C convention)
*** u2_cr_: read functions which never bail out.
*** u2_cs_: structures and definitions.
*** u2_ct_: tracing.
*** u2_cw_: direct jet calls (core noun convention)
*** u2_cx_: read functions which do bail out.
*** u2_cv_: arvo specific structures.
*** u2_cz_: memoization.
*** u3_ca_: fundamental allocators.
*** u3_cc_: constants.
*** u3_ce_: checkpointing.
*** u3_ch_: HAMT hash tables.
*** u3_ci_: noun constructors
*** u3_cj_: jets.
*** u3_ck*: direct jet calls (modern C convention)
*** u3_cm_: system management etc.
*** u3_cn_: nock interpreter.
*** u3_co_: fundamental macros.
*** u3_cq*: direct jet calls (archaic C convention)
*** u3_cr_: read functions which never bail out.
*** u3_cs_: structures and definitions.
*** u3_ct_: tracing.
*** u3_cw_: direct jet calls (core noun convention)
*** u3_cx_: read functions which do bail out.
*** u3_cv_: arvo specific structures.
*** u3_cz_: memoization.
***
*** u2_cr_, u2_cx_ functions use retain conventions; the caller
*** u3_cr_, u3_cx_ functions use retain conventions; the caller
*** retains ownership of passed-in nouns, the callee preserves
*** ownership of returned nouns.
***
@ -43,7 +44,7 @@
# include "c/motes.h"
# include "c/comd.h"
/** n: the u2 layer, definitions and data structures.
/** n: the u3 layer, definitions and data structures.
**/
# include "n/tune.h"
# include "n/noun.h"
@ -54,7 +55,7 @@
# include "n/arvo.h"
# include "n/glob.h"
/** g: the u2 layer, functions.
/** g: the u3 layer, functions.
**/
# include "g/a.h"
# include "g/h.h"
@ -68,7 +69,7 @@
# include "g/v.h"
# include "g/z.h"
/** j: the u2 layer, jets.
/** j: the u3 layer, jets.
**/
# include "j/k.h"
# include "j/w.h"

24
include/g/e.h Normal file
View File

@ -0,0 +1,24 @@
/* include/g/e.h
**
** This file is in the public domain.
*/
/** Functions.
**/
/* u3_ce_fault(): handle a memory event with libsigsegv protocol.
*/
static c3_i
u3_ce_fault(void* adr_v, c3_i ser_i);
/* u3_ce_load(): try to load image from checkpoint directory.
*/
u3_o
u3_ce_load(const c3_c* pax_c, c3_p adr_p, c3_w len_w)
/* u3_ce_make(): create new image with checkpoint.
*/
u3_o
u3_ce_make(const c3_c* pax_c);
/* u3_ce_sync(): save changes with checkpoint dir.
*/
u3_ce_sync(void);

View File

@ -19,8 +19,6 @@
/* u3_cs_arvo: modern arvo structure.
*/
typedef struct _u3_cs_arvo {
c3_d ent_d; // event counter
u3_noun yot; // cached gates
u3_noun now; // current time, as noun
u3_noun wen; // current time, as text
@ -43,9 +41,3 @@
};
} u3_cs_arvo;
/* u3_cs_home: complete system state.
*/
typedef struct _u3_cs_home {
u3_cs_road rod_u;
u3_cs_arvo arv_u;
} u3_cs_home;

View File

@ -24,6 +24,11 @@
extern u3_cs_dash u3_Dash;
# define u3D u3_Dash
/* u3_Pool / u3P: global memory control.
*/
extern u3_cs_pool u3_Pool;
# define u3P u3_Pool
/* u3_Code: memory code.
*/
#ifdef U3_MEMORY_DEBUG

View File

@ -117,17 +117,18 @@
*/
typedef struct _u3_cs_road {
struct _u3_cs_road* par_u; // parent road
struct _u3_cs_road* kid_u; // child road list
struct _u3_cs_road* nex_u; // sibling road
struct _u3_cs_road* now_u; // current road pointer
c3_w* cap_w; // top of transient region
c3_w* hat_w; // top of durable region
c3_w* mat_w; // bottom of transient region
c3_w* rut_w; // bottom of durable region
c3_w* cap_w; // top of transient region
c3_w* hat_w; // top of durable region
c3_w* mat_w; // bottom of transient region
c3_w* rut_w; // bottom of durable region
#if 0
c3_w* gar_w; // bottom of guard region (future)
c3_w* rag_w; // top of guard region (future)
c3_w pad_w[4]; // future interesting info
#endif

View File

@ -11,3 +11,7 @@
# define u3_leak_on(x) (u2_Code = x)
# define u3_leak_off (u2_Code = 0)
# endif
# define u3_cc_bits U2_OS_LoomBits // 28, max 29
# define u3_cc_page 12 // 16Kbyte pages
# define u3_cc_pages (1 << (u3_cc_bits - u3_cc_page)) // 2^16 pages

View File

@ -8,11 +8,9 @@
/* functions
*/
u3_noun
u3_cqa_gte(
u3_atom a,
u3_atom b)
u3_cqa_gte(u3_atom a, u3_atom b)
{
if ( u3_co_is_cat(a) && u3_co_is_cat(b) ) {
if ( u3_so(u3_co_is_cat(a)) && u3_so(u3_co_is_cat(b)) ) {
return u3_say(a >= b);
}
else {

View File

@ -11,7 +11,7 @@
u3_cqa_gth(u3_atom a,
u3_atom b)
{
if ( u3_co_is_cat(a) && u3_co_is_cat(b) ) {
if ( u3_so(u3_co_is_cat(a)) && u3_so(u3_co_is_cat(b)) ) {
return u3_say(a > b);
}
else {

View File

@ -8,10 +8,9 @@
/* functions
*/
u3_noun
u3_cqa_lte(u3_atom a,
u3_atom b)
u3_cqa_lte(u3_atom a, u3_atom b)
{
if ( u3_co_is_cat(a) && u3_co_is_cat(b) ) {
if ( u3_so(u3_co_is_cat(a)) && u3_so(u3_co_is_cat(b)) ) {
return u3_say(a <= b);
}
else {

View File

@ -11,7 +11,7 @@
u3_cqa_lth(u3_atom a,
u3_atom b)
{
if ( u3_co_is_cat(a) && u3_co_is_cat(b) ) {
if ( u3_so(u3_co_is_cat(a)) && u3_so(u3_co_is_cat(b)) ) {
return u3_say(a <= b);
}
else {

View File

@ -11,7 +11,7 @@ static u3_cs_harm _mood__hoon_gte_a[] = {{".2", u3_cwa_gte, u3_yes}, {}};
static u3_cs_harm _mood__hoon_gth_a[] = {{".2", u3_cwa_gth, u3_yes}, {}};
static u3_cs_harm _mood__hoon_lte_a[] = {{".2", u3_cwa_lte, u3_yes}, {}};
static u3_cs_harm _mood__hoon_lth_a[] = {{".2", u3_cwa_lth, u3_yes}, {}};
static u3_cs_harm _mood__hoon_mod_a[] = {{".2", u3_cwa_mod, u3_no}, {}};
static u3_cs_harm _mood__hoon_mod_a[] = {{".2", u3_cwa_mod, u3_yes}, {}};
static u3_cs_harm _mood__hoon_mul_a[] = {{".2", u3_cwa_mul, u3_yes}, {}};
static u3_cs_harm _mood__hoon_sub_a[] = {{".2", u3_cwa_sub, u3_yes}, {}};