mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
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:
parent
467504308a
commit
f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions
|
@ -6,12 +6,14 @@ PKGS += TOOL_DECODE
|
|||
TOOL_DECODE_FILES := $(wildcard tool/decode/*)
|
||||
TOOL_DECODE_HDRS = $(filter %.h,$(TOOL_DECODE_FILES))
|
||||
TOOL_DECODE_SRCS = $(filter %.c,$(TOOL_DECODE_FILES))
|
||||
TOOL_DECODE_COMS = $(TOOL_DECODE_OBJS:%.o=%.com)
|
||||
|
||||
TOOL_DECODE_OBJS = \
|
||||
$(TOOL_DECODE_SRCS:%=o/$(MODE)/%.zip.o) \
|
||||
$(TOOL_DECODE_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TOOL_DECODE_COMS = \
|
||||
$(TOOL_DECODE_SRCS:%.c=o/$(MODE)/%.com)
|
||||
|
||||
TOOL_DECODE_BINS = \
|
||||
$(TOOL_DECODE_COMS) \
|
||||
$(TOOL_DECODE_COMS:%=%.dbg)
|
||||
|
@ -37,6 +39,7 @@ TOOL_DECODE_DIRECTDEPS = \
|
|||
LIBC_UNICODE \
|
||||
LIBC_X \
|
||||
TOOL_DECODE_LIB \
|
||||
THIRD_PARTY_DTOA \
|
||||
THIRD_PARTY_GETOPT \
|
||||
THIRD_PARTY_XED
|
||||
|
||||
|
|
|
@ -108,7 +108,7 @@ static void printelfsegmentheader(int i) {
|
|||
firstnonnull(findnamebyid(kElfSegmentTypeNames, phdr->p_type),
|
||||
format(b1, "%#x", phdr->p_type)),
|
||||
"phdr->p_type");
|
||||
show(".long", recreateflags(kElfSegmentFlagNames, phdr->p_flags),
|
||||
show(".long", RecreateFlags(kElfSegmentFlagNames, phdr->p_flags),
|
||||
"phdr->p_flags");
|
||||
show(".quad", format(b1, "%#x", phdr->p_offset), "phdr->p_offset");
|
||||
show(".quad", format(b1, "%#x", phdr->p_vaddr), "phdr->p_vaddr");
|
||||
|
@ -140,7 +140,7 @@ static void printelfsectionheader(int i, char *shstrtab) {
|
|||
firstnonnull(findnamebyid(kElfSectionTypeNames, shdr->sh_type),
|
||||
format(b1, "%d", shdr->sh_type)),
|
||||
"shdr->sh_type");
|
||||
show(".long", recreateflags(kElfSectionFlagNames, shdr->sh_flags),
|
||||
show(".long", RecreateFlags(kElfSectionFlagNames, shdr->sh_flags),
|
||||
"shdr->sh_flags");
|
||||
show(".quad", format(b1, "%#x", shdr->sh_addr), "shdr->sh_addr");
|
||||
show(".quad", format(b1, "%#x", shdr->sh_offset), "shdr->sh_offset");
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/elf/def.h"
|
||||
#include "libc/elf/elf.h"
|
||||
#include "tool/decode/lib/elfidnames.h"
|
||||
|
||||
|
|
|
@ -17,17 +17,12 @@
|
|||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/alg/arraylist.h"
|
||||
#include "libc/alg/arraylist2.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "tool/decode/lib/flagger.h"
|
||||
|
||||
struct FlagNameBuf {
|
||||
size_t i, n;
|
||||
char *p;
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats numeric flags integer as symbolic code.
|
||||
*
|
||||
|
@ -35,28 +30,32 @@ struct FlagNameBuf {
|
|||
* @param id is the flags
|
||||
* @return NUL-terminated string that needs free()
|
||||
*/
|
||||
nodiscard char *recreateflags(const struct IdName *names, unsigned long id) {
|
||||
struct FlagNameBuf buf = {};
|
||||
char extrabuf[20];
|
||||
nodiscard char *RecreateFlags(const struct IdName *names, unsigned long id) {
|
||||
bool first;
|
||||
size_t bufi, bufn;
|
||||
char *bufp, extrabuf[20];
|
||||
bufi = 0;
|
||||
bufn = 0;
|
||||
bufp = NULL;
|
||||
first = true;
|
||||
for (; names->name; names++) {
|
||||
if ((id == 0 && names->id == 0) ||
|
||||
(id != 0 && names->id != 0 && (id & names->id) == names->id)) {
|
||||
id &= ~names->id;
|
||||
if (!first) {
|
||||
append(&buf, "|");
|
||||
APPEND(&bufp, &bufi, &bufn, "|");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
concat(&buf, names->name, strlen(names->name));
|
||||
CONCAT(&bufp, &bufi, &bufn, names->name, strlen(names->name));
|
||||
}
|
||||
}
|
||||
if (id) {
|
||||
if (buf.i) append(&buf, "|");
|
||||
concat(&buf, extrabuf, snprintf(extrabuf, sizeof(extrabuf), "%#x", id));
|
||||
} else if (!buf.i) {
|
||||
append(&buf, "0");
|
||||
if (bufi) APPEND(&bufp, &bufi, &bufn, "|");
|
||||
CONCAT(&bufp, &bufi, &bufn, extrabuf,
|
||||
snprintf(extrabuf, sizeof(extrabuf), "%#x", id));
|
||||
} else if (!bufi) {
|
||||
APPEND(&bufp, &bufi, &bufn, "0");
|
||||
}
|
||||
return buf.p;
|
||||
return bufp;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
char *recreateflags(const struct IdName *, unsigned long) nodiscard;
|
||||
char *RecreateFlags(const struct IdName *, unsigned long) nodiscard;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -49,7 +49,7 @@ kPollNamesRo:
|
|||
.previous
|
||||
|
||||
/ Mapping of poll() flags to their string names.
|
||||
/ @see recreateflags()
|
||||
/ @see RecreateFlags()
|
||||
.initbss 301,_init_kPollNames
|
||||
kPollNames:
|
||||
.rept .Lrows
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include "third_party/xed/x86.h"
|
||||
#include "tool/decode/lib/idname.h"
|
||||
|
||||
const struct IdName kXedErrorNames[] = {
|
||||
const struct IdName kXedErrorIdNames[] = {
|
||||
{XED_ERROR_NONE, "NONE"},
|
||||
{XED_ERROR_BUFFER_TOO_SHORT, "BUFFER_TOO_SHORT"},
|
||||
{XED_ERROR_GENERAL_ERROR, "GENERAL_ERROR"},
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
extern const struct IdName kXedErrorNames[];
|
||||
extern const struct IdName kXedErrorIdNames[];
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -19,12 +19,12 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/conv/conv.h"
|
||||
#include "libc/macho.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
@ -66,7 +66,7 @@ static void showmachoheader(void) {
|
|||
showinthex(macho->loadcount);
|
||||
showinthex(macho->loadsize);
|
||||
show(".long",
|
||||
firstnonnull(recreateflags(kMachoFlagNames, macho->flags),
|
||||
firstnonnull(RecreateFlags(kMachoFlagNames, macho->flags),
|
||||
format(b1, "%#x", macho->flags)),
|
||||
"macho->flags");
|
||||
showinthex(macho->__reserved);
|
||||
|
@ -107,16 +107,16 @@ static void showmacholoadsegment(unsigned i, struct MachoLoadSegment *loadseg) {
|
|||
showint64hex(loadseg->offset);
|
||||
showint64hex(loadseg->filesz);
|
||||
show(".long",
|
||||
firstnonnull(recreateflags(kMachoVmProtNames, loadseg->maxprot),
|
||||
firstnonnull(RecreateFlags(kMachoVmProtNames, loadseg->maxprot),
|
||||
format(b1, "%#x", loadseg->maxprot)),
|
||||
"loadseg->maxprot");
|
||||
show(".long",
|
||||
firstnonnull(recreateflags(kMachoVmProtNames, loadseg->initprot),
|
||||
firstnonnull(RecreateFlags(kMachoVmProtNames, loadseg->initprot),
|
||||
format(b1, "%#x", loadseg->initprot)),
|
||||
"loadseg->initprot");
|
||||
showinthex(loadseg->sectioncount);
|
||||
show(".long",
|
||||
firstnonnull(recreateflags(kMachoSegmentFlagNames, loadseg->flags),
|
||||
firstnonnull(RecreateFlags(kMachoSegmentFlagNames, loadseg->flags),
|
||||
format(b1, "%#x", loadseg->flags)),
|
||||
"loadseg->flags");
|
||||
for (unsigned j = 0; j < loadseg->sectioncount; ++j) {
|
||||
|
|
|
@ -94,6 +94,7 @@ void GetOpts(int argc, char *argv[]) {
|
|||
* e.g. A̸B̸C̸D̸ 41CCB8 42CCB8 43CCB8 44CCB8
|
||||
*
|
||||
* @see unicode.org/reports/tr11/#Definitions
|
||||
* @see https://www.compart.com/en/unicode/category/Sk (Modifier Symbol)
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
GetOpts(argc, argv);
|
||||
|
@ -109,7 +110,7 @@ int main(int argc, char *argv[]) {
|
|||
if (bit != 0x00AD &&
|
||||
((0x1160 <= bit && bit <= 0x11FF) ||
|
||||
(strcmp(category, "Me") == 0 || strcmp(category, "Mn") == 0 ||
|
||||
strcmp(category, "Cf") == 0 || strcmp(category, "Sk") == 0))) {
|
||||
strcmp(category, "Cf") == 0))) {
|
||||
maxbit = max(bit, maxbit);
|
||||
CHECK(bitabuilder_setbit(bitset, bit));
|
||||
}
|
||||
|
|
|
@ -48,8 +48,8 @@ static struct XedDecodedInst *ildreal(void *addr) {
|
|||
if (xed_instruction_length_decode(
|
||||
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_REAL), addr,
|
||||
XED_MAX_INSTRUCTION_BYTES) != XED_ERROR_NONE ||
|
||||
!xedd.decoded_length) {
|
||||
xedd.decoded_length = 1;
|
||||
!xedd.length) {
|
||||
xedd.length = 1;
|
||||
}
|
||||
return &xedd;
|
||||
}
|
||||
|
@ -115,14 +115,14 @@ static void showdosstub(void) {
|
|||
pe = min(pe, p + mzsize - XED_MAX_INSTRUCTION_BYTES);
|
||||
while (p < pe) {
|
||||
struct XedDecodedInst *inst = ildreal(p);
|
||||
if (p + inst->decoded_length > pe) break;
|
||||
if (p + inst->length > pe) break;
|
||||
printf("\t.byte\t");
|
||||
for (unsigned i = 0; i < inst->decoded_length; ++i) {
|
||||
for (unsigned i = 0; i < inst->length; ++i) {
|
||||
if (i) printf(",");
|
||||
printf("%#hhx", xed_decoded_inst_get_byte(inst, i));
|
||||
printf("%#hhx", inst->bytes[i]);
|
||||
}
|
||||
printf("\n");
|
||||
p += inst->decoded_length;
|
||||
p += inst->length;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ static void showpeoptionalheader(struct NtImageOptionalHeader *opt) {
|
|||
format(b1, "%#hx", opt->Subsystem)),
|
||||
"opt->Subsystem");
|
||||
show(".short",
|
||||
firstnonnull(recreateflags(kNtImageDllcharacteristicNames,
|
||||
firstnonnull(RecreateFlags(kNtImageDllcharacteristicNames,
|
||||
opt->DllCharacteristics),
|
||||
format(b1, "%#hx", opt->DllCharacteristics)),
|
||||
"opt->DllCharacteristics");
|
||||
|
@ -188,7 +188,7 @@ static void showpeheader(struct NtImageNtHeaders *pe) {
|
|||
showint(pe->FileHeader.NumberOfSymbols);
|
||||
showshort(pe->FileHeader.SizeOfOptionalHeader);
|
||||
show(".short",
|
||||
firstnonnull(recreateflags(kNtImageCharacteristicNames,
|
||||
firstnonnull(RecreateFlags(kNtImageCharacteristicNames,
|
||||
pe->FileHeader.Characteristics),
|
||||
format(b1, "%#hx", pe->FileHeader.Characteristics)),
|
||||
"pe->FileHeader.Characteristics");
|
||||
|
|
|
@ -46,7 +46,7 @@ noreturn void ShowUsage(int rc, FILE *f) {
|
|||
size_t i;
|
||||
fputs("Usage: ", f);
|
||||
fputs(program_invocation_name, f);
|
||||
fputs(" [-r] [-m MODE] HEX\n MODE ∊ {", f);
|
||||
fputs(" [-rl] [-m MODE] HEX\n MODE ∊ {", f);
|
||||
fputs(kXedModeNames[0].name, f);
|
||||
for (i = 1; i < ARRAYLEN(kXedModeNames); ++i) {
|
||||
fputc(',', f);
|
||||
|
@ -71,11 +71,14 @@ void SetMachineMode(const char *s) {
|
|||
void GetOpts(int argc, char *argv[]) {
|
||||
int opt;
|
||||
g_mode = XED_MACHINE_MODE_LONG_64;
|
||||
while ((opt = getopt(argc, argv, "?hrm:")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "?hrlm:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'r':
|
||||
g_mode = XED_MACHINE_MODE_REAL;
|
||||
break;
|
||||
case 'l':
|
||||
g_mode = XED_MACHINE_MODE_LEGACY_32;
|
||||
break;
|
||||
case 'm':
|
||||
SetMachineMode(optarg);
|
||||
break;
|
||||
|
@ -106,6 +109,12 @@ void ShowField(const char *name, uint64_t value) {
|
|||
}
|
||||
}
|
||||
|
||||
void ShowField2(const char *name, uint64_t value, bool cond) {
|
||||
if (value || cond) {
|
||||
printf("/\t%-20s = %#lx\n", name, value);
|
||||
}
|
||||
}
|
||||
|
||||
void ShowOffset(const char *name, uint64_t off) {
|
||||
printf("/\t%-20s = %#lx\n", name, off);
|
||||
}
|
||||
|
@ -129,56 +138,20 @@ int main(int argc, char *argv[]) {
|
|||
if ((err = xed_instruction_length_decode(&g_xedd, buf, k)) !=
|
||||
XED_ERROR_NONE) {
|
||||
fputs("XED_ERROR_", stderr);
|
||||
fputs(findnamebyid(kXedErrorNames, err), stderr);
|
||||
fputs(findnamebyid(kXedErrorIdNames, err), stderr);
|
||||
fputc('\n', stderr);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#define SHOWOP(F) ShowField(#F, g_xedd.operands.F)
|
||||
SHOWOP(amd3dnow);
|
||||
SHOWOP(asz);
|
||||
SHOWOP(bcrc);
|
||||
SHOWOP(chip);
|
||||
SHOWOP(cldemote);
|
||||
SHOWOP(disp);
|
||||
SHOWOP(disp_width);
|
||||
#define SHOWOP(F) ShowField(#F, g_xedd.op.F)
|
||||
#define SHOWOP2(F, C) ShowField2(#F, g_xedd.op.F, C)
|
||||
|
||||
printf("/\t%-20s = %d\n", "length", g_xedd.length);
|
||||
SHOWOP(error);
|
||||
SHOWOP(esrc);
|
||||
SHOWOP(first_f2f3);
|
||||
SHOWOP(modrm);
|
||||
SHOWOP(sib);
|
||||
SHOWOP(has_modrm);
|
||||
SHOWOP(has_sib);
|
||||
SHOWOP(hint);
|
||||
SHOWOP(ild_f2);
|
||||
SHOWOP(ild_f3);
|
||||
SHOWOP(ild_seg);
|
||||
SHOWOP(imm1_bytes);
|
||||
SHOWOP(imm_width);
|
||||
SHOWOP(last_f2f3);
|
||||
SHOWOP(llrc);
|
||||
|
||||
SHOWOP(lock);
|
||||
SHOWOP(map);
|
||||
SHOWOP(mask);
|
||||
SHOWOP(max_bytes);
|
||||
SHOWOP(mod);
|
||||
SHOWOP(mode);
|
||||
SHOWOP(mode_first_prefix);
|
||||
SHOWOP(nominal_opcode);
|
||||
SHOWOP(nprefixes);
|
||||
SHOWOP(nrexes);
|
||||
SHOWOP(nseg_prefixes);
|
||||
SHOWOP(osz);
|
||||
SHOWOP(out_of_bytes);
|
||||
SHOWOP(pos_disp);
|
||||
SHOWOP(pos_imm);
|
||||
SHOWOP(pos_imm1);
|
||||
SHOWOP(pos_modrm);
|
||||
SHOWOP(pos_nominal_opcode);
|
||||
SHOWOP(pos_sib);
|
||||
SHOWOP(prefix66);
|
||||
SHOWOP(realmode);
|
||||
SHOWOP(reg);
|
||||
SHOWOP(asz);
|
||||
SHOWOP(rep);
|
||||
SHOWOP(rex);
|
||||
SHOWOP(rexb);
|
||||
|
@ -186,19 +159,70 @@ int main(int argc, char *argv[]) {
|
|||
SHOWOP(rexrr);
|
||||
SHOWOP(rexw);
|
||||
SHOWOP(rexx);
|
||||
SHOWOP(rm);
|
||||
|
||||
SHOWOP(map);
|
||||
SHOWOP(opcode);
|
||||
|
||||
if (g_xedd.op.has_modrm) {
|
||||
printf("\n");
|
||||
printf("/\t%-20s = 0b%02hhb (%d)\n", "mod", g_xedd.op.mod, g_xedd.op.mod);
|
||||
printf("/\t%-20s = 0b%03hhb (%d)\n", "reg", g_xedd.op.reg, g_xedd.op.reg);
|
||||
printf("/\t%-20s = 0b%03hhb (%d)\n", "rm", g_xedd.op.rm, g_xedd.op.rm);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (g_xedd.op.has_sib) {
|
||||
printf("/\t%-20s = 0b%02hhb (%d)\n", "scale", xed_sib_scale(g_xedd.op.sib),
|
||||
xed_sib_scale(g_xedd.op.sib));
|
||||
printf("/\t%-20s = 0b%03hhb (%d)\n", "index", xed_sib_index(g_xedd.op.sib),
|
||||
xed_sib_index(g_xedd.op.sib));
|
||||
printf("/\t%-20s = 0b%03hhb (%d)\n", "base", xed_sib_base(g_xedd.op.sib),
|
||||
xed_sib_base(g_xedd.op.sib));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
SHOWOP2(disp, !!g_xedd.op.pos_disp);
|
||||
SHOWOP2(uimm0, !!g_xedd.op.pos_imm);
|
||||
SHOWOP2(uimm1, !!g_xedd.op.pos_imm1);
|
||||
|
||||
SHOWOP(mode);
|
||||
SHOWOP(amd3dnow);
|
||||
SHOWOP(bcrc);
|
||||
SHOWOP(disp_width);
|
||||
SHOWOP(first_f2f3);
|
||||
SHOWOP(hint);
|
||||
SHOWOP(ild_f2);
|
||||
SHOWOP(ild_f3);
|
||||
SHOWOP(ild_seg);
|
||||
SHOWOP(imm1_bytes);
|
||||
SHOWOP(imm_width);
|
||||
SHOWOP(imm_signed);
|
||||
SHOWOP(last_f2f3);
|
||||
SHOWOP(llrc);
|
||||
SHOWOP(mask);
|
||||
SHOWOP(prefix66);
|
||||
SHOWOP(max_bytes);
|
||||
SHOWOP(mode_first_prefix);
|
||||
SHOWOP(nprefixes);
|
||||
SHOWOP(nrexes);
|
||||
SHOWOP(nseg_prefixes);
|
||||
SHOWOP(out_of_bytes);
|
||||
SHOWOP(pos_disp);
|
||||
SHOWOP(pos_imm);
|
||||
SHOWOP(pos_imm1);
|
||||
SHOWOP(pos_modrm);
|
||||
SHOWOP(pos_opcode);
|
||||
SHOWOP(pos_sib);
|
||||
SHOWOP(realmode);
|
||||
SHOWOP(seg_ovd);
|
||||
SHOWOP(srm);
|
||||
SHOWOP(ubit);
|
||||
SHOWOP(uimm0);
|
||||
SHOWOP(uimm1);
|
||||
SHOWOP(vex_prefix);
|
||||
SHOWOP(vexdest210);
|
||||
SHOWOP(vexdest3);
|
||||
SHOWOP(vexdest4);
|
||||
SHOWOP(vexvalid);
|
||||
SHOWOP(vl);
|
||||
SHOWOP(wbnoinvd);
|
||||
SHOWOP(zeroing);
|
||||
|
||||
return 0;
|
||||
|
|
135
tool/decode/x87.c
Normal file
135
tool/decode/x87.c
Normal file
|
@ -0,0 +1,135 @@
|
|||
/*-*- 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/math.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/dtoa/dtoa.h"
|
||||
|
||||
const char kConfig[] = "\
|
||||
/* FPU Control Word (x87) Exception Masks\n\
|
||||
@see Intel Manual V1 §8.1.5\n\
|
||||
IM: Invalid Operation ───────────────┐\n\
|
||||
DM: Denormal Operand ───────────────┐│\n\
|
||||
ZM: Zero Divide ───────────────────┐││\n\
|
||||
OM: Overflow ─────────────────────┐│││\n\
|
||||
UM: Underflow ───────────────────┐││││\n\
|
||||
PM: Precision ──────────────────┐│││││\n\
|
||||
PC: Precision Control ────────┐ ││││││\n\
|
||||
{float,∅,double,long double} │ ││││││\n\
|
||||
RC: Rounding Control ───────┐ │ ││││││\n\
|
||||
{even, →-∞, →+∞, →0} │┌┤ ││││││\n\
|
||||
┌┤││ ││││││\n\
|
||||
d││││rr││││││*/\n\
|
||||
";
|
||||
|
||||
const char kStatus[] = "\
|
||||
/* FPU Status Word (x87)\n\
|
||||
@see Intel Manual V1 §8.1.3\n\
|
||||
IE: Invalid Operation ────────────────┐\n\
|
||||
DE: Denormalized Operand ────────────┐│\n\
|
||||
ZE: Zero Divide ────────────────────┐││\n\
|
||||
OE: Overflow Flag ─────────────────┐│││\n\
|
||||
UE: Underflow Flag ───────────────┐││││\n\
|
||||
PE: Precision Flag ──────────────┐│││││\n\
|
||||
SF: Stack Fault ────────────────┐││││││\n\
|
||||
ES: Exception Summary Status ──┐│││││││\n\
|
||||
C0-3: Condition Codes ──┬────┐ ││││││││\n\
|
||||
TOP of Stack Pointer ─────┐ │ ││││││││\n\
|
||||
B: FPU Busy ───────────┐│ │ │ ││││││││\n\
|
||||
││┌┴┐┌┼┐││││││││\n\
|
||||
│↓│ │↓↓↓││││││││*/\n\
|
||||
";
|
||||
|
||||
const char kRegister[] = "\
|
||||
/\t┌sign\n\
|
||||
/\t│ ┌exponent\n\
|
||||
/\t│ │ ┌intpart\n\
|
||||
/\t│ │ │ ┌fraction\n\
|
||||
/\t│ │ │ │\n\
|
||||
/\t│┌┴────────────┐│┌┴────────────────────────────────────────────────────────────┐\n";
|
||||
|
||||
void PrintRegister(long double x) {
|
||||
char buf[32];
|
||||
uint16_t lo, hi;
|
||||
memcpy(buf, &x, sizeof(x));
|
||||
memcpy(&lo, &buf[0], sizeof(lo));
|
||||
memcpy(&hi, &buf[8], sizeof(hi));
|
||||
printf("/\t%016lb%064lb %s\n", hi, lo, g_fmt(buf, x));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
double d;
|
||||
unsigned short sw;
|
||||
long double st0, st1;
|
||||
|
||||
#define ST0 1
|
||||
#define ST1 2
|
||||
d = 0;
|
||||
st0 = ST0;
|
||||
st1 = ST1;
|
||||
asm("fstl\t%1\n\t"
|
||||
"fstsw"
|
||||
: "=a"(sw), "=m"(d) /*, "+t"(st0), "+u"(st1) */);
|
||||
printf("\n%s%016b\n\n%s", kStatus, sw, kRegister);
|
||||
PrintRegister(st0);
|
||||
PrintRegister(st1);
|
||||
printf("/\t%-80s %s\n", "d", gc(xdtoa(d)));
|
||||
|
||||
#if 0
|
||||
#define ST0 2e18L
|
||||
#define ST1 0.0000000000000123L
|
||||
st0 = ST0;
|
||||
st1 = ST1;
|
||||
asm("fldpi\n\t"
|
||||
"fadd\t%%st\n\t"
|
||||
"fxch\n2:\t"
|
||||
"fprem1\n\t"
|
||||
"fnstsw\n\t"
|
||||
"test\t$4,%%ah\n\t"
|
||||
"jnz\t2b\n\t"
|
||||
"fstp\t%%st(1)\n\t"
|
||||
"fsin\n\t"
|
||||
"fstsw"
|
||||
: "=a"(sw), "+t"(st0), "+u"(st1));
|
||||
printf("\n%s%016b\n\n%s", kStatus, sw, kRegister);
|
||||
PrintRegister(st0);
|
||||
PrintRegister(st1);
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
st0 = ST0;
|
||||
st1 = ST1;
|
||||
asm("fsin\n\t"
|
||||
"fstsw"
|
||||
: "=a"(sw), "+t"(st0), "+u"(st1));
|
||||
printf("\n%s%016b\n\n%s", kStatus, sw, kRegister);
|
||||
PrintRegister(st0);
|
||||
PrintRegister(st1);
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
printf("\n");
|
||||
PrintRegister(sinl(remainderl(2e40L, M_PI * 2)));
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
#include "libc/conv/conv.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/crc32.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
@ -93,12 +94,12 @@ void showcompressmethod(uint16_t compressmethod) {
|
|||
|
||||
void showextrantfs(uint8_t *ntfs) {
|
||||
struct timespec mtime, atime, ctime;
|
||||
filetimetotimespec(
|
||||
&mtime, (struct NtFileTime){read32le(ntfs + 8), read32le(ntfs + 12)});
|
||||
filetimetotimespec(
|
||||
&atime, (struct NtFileTime){read32le(ntfs + 16), read32le(ntfs + 20)});
|
||||
filetimetotimespec(
|
||||
&ctime, (struct NtFileTime){read32le(ntfs + 24), read32le(ntfs + 30)});
|
||||
mtime = filetimetotimespec(
|
||||
(struct NtFileTime){read32le(ntfs + 8), read32le(ntfs + 12)});
|
||||
atime = filetimetotimespec(
|
||||
(struct NtFileTime){read32le(ntfs + 16), read32le(ntfs + 20)});
|
||||
ctime = filetimetotimespec(
|
||||
(struct NtFileTime){read32le(ntfs + 24), read32le(ntfs + 30)});
|
||||
show(".long", gc(xasprintf("%d", read32le(ntfs))), "ntfs reserved");
|
||||
show(".short", gc(xasprintf("0x%04x", read16le(ntfs + 4))),
|
||||
"ntfs attribute tag value #1");
|
||||
|
@ -131,11 +132,11 @@ void showexternalattributes(uint8_t *cf) {
|
|||
uint32_t ea;
|
||||
ea = ZIP_CFILE_EXTERNALATTRIBUTES(cf);
|
||||
if (ZIP_CFILE_FILEATTRCOMPAT(cf) == kZipOsUnix) {
|
||||
show(".short", recreateflags(kNtFileFlagNames, ea & 0xffff),
|
||||
show(".short", RecreateFlags(kNtFileFlagNames, ea & 0xffff),
|
||||
"dos file flags");
|
||||
show(".short", format(b1, "%#o", ea >> 16), "st_mode");
|
||||
} else {
|
||||
show(".long", recreateflags(kNtFileFlagNames, ea), "externalattributes");
|
||||
show(".long", RecreateFlags(kNtFileFlagNames, ea), "externalattributes");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -241,7 +242,7 @@ void showcentralfileheader(uint8_t *cf) {
|
|||
"bytes"));
|
||||
show(".short", format(b1, "%hu", ZIP_CFILE_DISK(cf)), "disk");
|
||||
show(".short",
|
||||
recreateflags(kZipIattrNames, ZIP_CFILE_INTERNALATTRIBUTES(cf)),
|
||||
RecreateFlags(kZipIattrNames, ZIP_CFILE_INTERNALATTRIBUTES(cf)),
|
||||
"internalattributes");
|
||||
showexternalattributes(cf);
|
||||
show(".long", format(b1, "%u", ZIP_CFILE_OFFSET(cf)), "lfile hdr offset");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue