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

@ -18,15 +18,16 @@
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/notice.inc"
.source __FILE__
.bss
.align 8
g_rando:.quad 0
.endobj g_rando,globl,hidden
g_rando:
.quad 0
.endobj g_rando,globl
.previous
.init.start 100,_init_g_rando
movq $1,g_rando(%rip)
movb $1,g_rando(%rip)
.init.end 100,_init_g_rando
.source __FILE__

View file

@ -1,7 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_LCG_H_
#define COSMOPOLITAN_LIBC_LCG_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline uint64_t KnuthLinearCongruentialGenerator(uint64_t prev[1]) {
/* Knuth, D.E., "The Art of Computer Programming," Vol 2,
@ -11,6 +10,5 @@ forceinline uint64_t KnuthLinearCongruentialGenerator(uint64_t prev[1]) {
return prev[0];
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LCG_H_ */

View file

@ -28,4 +28,6 @@
* seed at startup by default, unless srand() is called. This makes it
* useful in cases where deterministic behavior is needed.
*/
int(rand)(void) { return KnuthLinearCongruentialGenerator(&g_rando) >> 33; }
int rand(void) {
return KnuthLinearCongruentialGenerator(&g_rando) >> 33;
}

View file

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_RAND_RAND_H_
#define COSMOPOLITAN_LIBC_RAND_RAND_H_
#include "libc/ncabi.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
@ -26,11 +25,6 @@ int64_t winrandish(void);
uint64_t rdrand(void);
float randf(void);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
NCABI_DECLARE_0(NCABI_NOPRUNE, int, __rand, "rand")
#define rand() __rand()
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RAND_RAND_H_ */

View file

@ -22,16 +22,18 @@
#include "libc/str/str.h"
/**
* Fills memory with random bytes.
* Fills memory with random bytes, e.g.
*
* @param seed can be rand64
* @param reseed is number of bytes between seed() calls
* @return buf
* char buf[1024];
* rngset(buf, sizeof(buf), rand64, -1);
*
* @param seed can be rand64() and is always called at least once
* @param reseed is bytes between seed() calls and -1 disables it
* @return original buf
*/
void *rngset(void *buf, size_t size, uint64_t (*seed)(void), size_t reseed) {
void *rngset(void *buf, size_t size, uint64_t seed(void), size_t reseed) {
unsigned char *p;
uint64_t i, x, state;
i = 0;
p = buf;
state = seed();
for (i = 0; size - i >= sizeof(x); i += sizeof(x)) {

View file

@ -5,24 +5,11 @@
#define kMarsagliaXorshift32Seed 2463534242
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline uint64_t MarsagliaXorshift64(uint64_t state[hasatleast 1]) {
uint64_t x = state[0];
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
state[0] = x;
return x;
}
forceinline uint32_t MarsagliaXorshift32(uint32_t state[hasatleast 1]) {
uint32_t x = state[0];
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
state[0] = x;
return x;
}
uint32_t MarsagliaXorshift32(uint32_t[hasatleast 1]);
uint64_t MarsagliaXorshift64(uint64_t[hasatleast 1]);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RAND_XORSHIFT_H_ */

View file

@ -17,6 +17,13 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/rand/internal.h"
#include "libc/rand/xorshift.h"
uint64_t g_rando;
uint32_t MarsagliaXorshift32(uint32_t state[hasatleast 1]) {
uint32_t x = state[0];
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
state[0] = x;
return x;
}

29
libc/rand/xorshift64.c Normal file
View file

@ -0,0 +1,29 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/rand/xorshift.h"
uint64_t MarsagliaXorshift64(uint64_t state[hasatleast 1]) {
uint64_t x = state[0];
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
state[0] = x;
return x;
}