Add x86_64-linux-gnu emulator

I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
This commit is contained in:
Justine Tunney 2020-08-25 04:23:25 -07:00
parent 467504308a
commit f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions

View file

@ -21,45 +21,46 @@
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/nt/files.h"
#include "libc/sysv/errfuns.h"
#define DBUFSIZ 1460 /* tcp ethernet frame < -Wframe-larger-than=4096 */
struct dfile {
struct VdprintfState {
int n;
int fd;
unsigned idx;
unsigned toto;
unsigned char buf[DBUFSIZ];
unsigned char buf[1024];
};
static int vdprintf_flush(struct dfile *df) {
ssize_t wrote;
do {
wrote = write(df->fd, &df->buf[0], df->idx);
if (wrote == -1) return -1;
df->toto += (unsigned)wrote;
df->idx -= (unsigned)wrote;
if (df->toto > INT_MAX) return eoverflow();
} while (df->idx);
static int vdprintf_flush(struct VdprintfState *df, int n) {
int i, rc;
for (i = 0; i < n; i += rc) {
if ((rc = write(df->fd, df->buf + i, n - i)) == -1) {
return -1;
}
}
return 0;
}
static int vdprintfputchar(unsigned char c, struct dfile *df) {
df->buf[df->idx++] = c;
if (df->idx == DBUFSIZ && vdprintf_flush(df) == -1) return -1;
return 0;
static int vdprintfputchar(int c, struct VdprintfState *df) {
df->buf[df->n++ & (ARRAYLEN(df->buf) - 1)] = c & 0xff;
if ((df->n & (ARRAYLEN(df->buf) - 1))) {
return 0;
} else {
return vdprintf_flush(df, ARRAYLEN(df->buf));
}
}
/**
* Formats string directly to system i/o device.
*/
int(vdprintf)(int fd, const char *fmt, va_list va) {
struct dfile df;
struct VdprintfState df;
df.n = 0;
df.fd = fd;
df.idx = 0;
df.toto = 0;
if (palandprintf(vdprintfputchar, &df, fmt, va) == -1) return -1;
if (df.idx && vdprintf_flush(&df) == -1) return -1;
return df.toto;
if (palandprintf(vdprintfputchar, &df, fmt, va) != -1 ||
vdprintf_flush(&df, df.n & (ARRAYLEN(df.buf) - 1)) != -1) {
return df.n;
} else {
return -1;
}
}