cosmopolitan/tool/build/lib/machine.h
Justine Tunney 39bf41f4eb Make numerous improvements
- Python static hello world now 1.8mb
- Python static fully loaded now 10mb
- Python HTTPS client now uses MbedTLS
- Python REPL now completes import stmts
- Increase stack size for Python for now
- Begin synthesizing posixpath and ntpath
- Restore Python \N{UNICODE NAME} support
- Restore Python NFKD symbol normalization
- Add optimized code path for Intel SHA-NI
- Get more Python unit tests passing faster
- Get Python help() pagination working on NT
- Python hashlib now supports MbedTLS PBKDF2
- Make memcpy/memmove/memcmp/bcmp/etc. faster
- Add Mersenne Twister and Vigna to LIBC_RAND
- Provide privileged __printf() for error code
- Fix zipos opendir() so that it reports ENOTDIR
- Add basic chmod() implementation for Windows NT
- Add Cosmo's best functions to Python cosmo module
- Pin function trace indent depth to that of caller
- Show memory diagram on invalid access in MODE=dbg
- Differentiate stack overflow on crash in MODE=dbg
- Add stb_truetype and tools for analyzing font files
- Upgrade to UNICODE 13 and reduce its binary footprint
- COMPILE.COM now logs resource usage of build commands
- Start implementing basic poll() support on bare metal
- Set getauxval(AT_EXECFN) to GetModuleFileName() on NT
- Add descriptions to strerror() in non-TINY build modes
- Add COUNTBRANCH() macro to help with micro-optimizations
- Make error / backtrace / asan / memory code more unbreakable
- Add fast perfect C implementation of μ-Law and a-Law audio codecs
- Make strtol() functions consistent with other libc implementations
- Improve Linenoise implementation (see also github.com/jart/bestline)
- COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 01:52:34 -07:00

189 lines
5.1 KiB
C

#ifndef COSMOPOLITAN_TOOL_BUILD_LIB_MACHINE_H_
#define COSMOPOLITAN_TOOL_BUILD_LIB_MACHINE_H_
#include "libc/runtime/runtime.h"
#include "third_party/xed/x86.h"
#include "tool/build/lib/fds.h"
#define kMachineHalt -1
#define kMachineDecodeError -2
#define kMachineUndefinedInstruction -3
#define kMachineSegmentationFault -4
#define kMachineExit -5
#define kMachineDivideError -6
#define kMachineFpuException -7
#define kMachineProtectionFault -8
#define kMachineSimdException -9
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct Machine {
struct XedDecodedInst *xedd;
uint64_t ip;
uint8_t cs[8];
uint8_t ss[8];
uint64_t codevirt;
uint8_t *codehost;
uint32_t mode;
uint32_t flags;
uint32_t tlbindex;
uint32_t stashsize;
int64_t stashaddr;
int64_t readaddr;
int64_t writeaddr;
uint32_t readsize;
uint32_t writesize;
union {
uint8_t reg[16][8];
struct {
uint8_t ax[8];
uint8_t cx[8];
uint8_t dx[8];
uint8_t bx[8];
uint8_t sp[8];
uint8_t bp[8];
uint8_t si[8];
uint8_t di[8];
uint8_t r8[8];
uint8_t r9[8];
uint8_t r10[8];
uint8_t r11[8];
uint8_t r12[8];
uint8_t r13[8];
uint8_t r14[8];
uint8_t r15[8];
};
};
struct MachineTlb {
int64_t virt;
uint64_t entry;
} tlb[16];
struct MachineReal {
size_t i, n;
uint8_t *p;
} real;
uint64_t cr3;
uint8_t xmm[16][16];
uint8_t es[8];
uint8_t ds[8];
uint8_t fs[8];
uint8_t gs[8];
struct MachineFpu {
long double st[8];
union {
uint32_t cw;
struct {
unsigned im : 1; // invalid operation mask
unsigned dm : 1; // denormal operand mask
unsigned zm : 1; // zero divide mask
unsigned om : 1; // overflow mask
unsigned um : 1; // underflow mask
unsigned pm : 1; // precision mask
unsigned _p1 : 2; // reserved
unsigned pc : 2; // precision: 32,∅,64,80
unsigned rc : 2; // rounding: even,→-∞,→+∞,→0
};
};
union {
uint32_t sw;
struct {
unsigned ie : 1; // invalid operation
unsigned de : 1; // denormalized operand
unsigned ze : 1; // zero divide
unsigned oe : 1; // overflow
unsigned ue : 1; // underflow
unsigned pe : 1; // precision
unsigned sf : 1; // stack fault
unsigned es : 1; // exception summary status
unsigned c0 : 1; // condition 0
unsigned c1 : 1; // condition 1
unsigned c2 : 1; // condition 2
unsigned sp : 3; // top stack
unsigned c3 : 1; // condition 3
unsigned bf : 1; // busy flag
};
};
int tw;
int op;
int64_t ip;
int64_t dp;
} fpu;
struct MachineSse {
union {
uint32_t mxcsr;
struct {
unsigned ie : 1; // invalid operation flag
unsigned de : 1; // denormal flag
unsigned ze : 1; // divide by zero flag
unsigned oe : 1; // overflow flag
unsigned ue : 1; // underflow flag
unsigned pe : 1; // precision flag
unsigned daz : 1; // denormals are zeros
unsigned im : 1; // invalid operation mask
unsigned dm : 1; // denormal mask
unsigned zm : 1; // divide by zero mask
unsigned om : 1; // overflow mask
unsigned um : 1; // underflow mask
unsigned pm : 1; // precision mask
unsigned rc : 2; // rounding control
unsigned ftz : 1; // flush to zero
};
};
} sse;
uint64_t cr0;
uint64_t cr2;
uint64_t cr4;
uint64_t gdt_base;
uint64_t idt_base;
uint16_t gdt_limit;
uint16_t idt_limit;
struct MachineRealFree {
uint64_t i;
uint64_t n;
struct MachineRealFree *next;
} * realfree;
struct FreeList {
uint32_t i;
void *p[6];
} freelist;
struct MachineMemstat {
int freed;
int resizes;
int reserved;
int committed;
int allocated;
int reclaimed;
int pagetables;
} memstat;
int64_t brk;
int64_t bofram[2];
jmp_buf onhalt;
int64_t faultaddr;
bool dlab;
bool ismetal;
struct MachineFds fds;
uint8_t stash[4096];
uint8_t icache[1024][40];
void (*onbinbase)(struct Machine *);
void (*onlongbranch)(struct Machine *);
} forcealign(64);
struct Machine *NewMachine(void) nodiscard;
void FreeMachine(struct Machine *);
void ResetMem(struct Machine *);
void ResetCpu(struct Machine *);
void ResetTlb(struct Machine *);
void ResetInstructionCache(struct Machine *);
void LoadInstruction(struct Machine *);
void ExecuteInstruction(struct Machine *);
long AllocateLinearPage(struct Machine *);
long AllocateLinearPageRaw(struct Machine *);
int ReserveReal(struct Machine *, size_t);
int ReserveVirtual(struct Machine *, int64_t, size_t, uint64_t);
char *FormatPml4t(struct Machine *) nodiscard;
int64_t FindVirtual(struct Machine *, int64_t, size_t);
int FreeVirtual(struct Machine *, int64_t, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TOOL_BUILD_LIB_MACHINE_H_ */