mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-28 13:30:29 +00:00
Make improvements
- Emulator can now test the αcτµαlly pδrταblε εxεcµταblε bootloader - Whipped up a webserver named redbean. It services 150k requests per second on a single core. Bundling assets inside zip enables extremely fast serving for two reasons. The first is that zip central directory lookups go faster than stat() system calls. The second is that both zip and gzip content-encoding use DEFLATE, therefore, compressed responses can be served via the sendfile() system call which does an in-kernel copy directly from the zip executable structure. Also note that red bean zip executables can be deployed easily to all platforms, since these native executables work on Linux, Mac, BSD, and Windows. - Address sanitizer now works very well
This commit is contained in:
parent
7327c345f9
commit
416fd86676
230 changed files with 9835 additions and 5682 deletions
|
@ -21,6 +21,8 @@
|
|||
#include "libc/log/log.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "tool/build/lib/address.h"
|
||||
#include "tool/build/lib/alu.h"
|
||||
#include "tool/build/lib/endian.h"
|
||||
#include "tool/build/lib/flags.h"
|
||||
|
@ -55,7 +57,7 @@ static void WriteInt(uint8_t p[8], uint64_t x, unsigned long w) {
|
|||
Write16(p, x);
|
||||
break;
|
||||
case 2:
|
||||
Write64(p, x);
|
||||
Write64(p, x & 0xffffffff);
|
||||
break;
|
||||
case 3:
|
||||
Write64(p, x);
|
||||
|
@ -65,164 +67,220 @@ static void WriteInt(uint8_t p[8], uint64_t x, unsigned long w) {
|
|||
}
|
||||
}
|
||||
|
||||
void OpString(struct Machine *m, uint32_t rde, int op) {
|
||||
static void AddDi(struct Machine *m, uint32_t rde, uint64_t x) {
|
||||
switch (Eamode(rde)) {
|
||||
case XED_MODE_LONG:
|
||||
Write64(m->di, Read64(m->di) + x);
|
||||
return;
|
||||
case XED_MODE_LEGACY:
|
||||
Write64(m->di, (Read32(m->di) + x) & 0xffffffff);
|
||||
return;
|
||||
case XED_MODE_REAL:
|
||||
Write16(m->di, Read16(m->di) + x);
|
||||
return;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
static void AddSi(struct Machine *m, uint32_t rde, uint64_t x) {
|
||||
switch (Eamode(rde)) {
|
||||
case XED_MODE_LONG:
|
||||
Write64(m->si, Read64(m->si) + x);
|
||||
return;
|
||||
case XED_MODE_LEGACY:
|
||||
Write64(m->si, (Read32(m->si) + x) & 0xffffffff);
|
||||
return;
|
||||
case XED_MODE_REAL:
|
||||
Write16(m->si, Read16(m->si) + x);
|
||||
return;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t ReadCx(struct Machine *m, uint32_t rde) {
|
||||
switch (Eamode(rde)) {
|
||||
case XED_MODE_LONG:
|
||||
return Read64(m->cx);
|
||||
case XED_MODE_LEGACY:
|
||||
return Read32(m->cx);
|
||||
case XED_MODE_REAL:
|
||||
return Read16(m->cx);
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t SubtractCx(struct Machine *m, uint32_t rde, uint64_t x) {
|
||||
uint64_t cx;
|
||||
cx = Read64(m->cx) - x;
|
||||
if (Eamode(rde) != XED_MODE_REAL) {
|
||||
if (Eamode(rde) == XED_MODE_LEGACY) {
|
||||
cx &= 0xffffffff;
|
||||
}
|
||||
Write64(m->cx, cx);
|
||||
} else {
|
||||
cx &= 0xffff;
|
||||
Write16(m->cx, cx);
|
||||
}
|
||||
return cx;
|
||||
}
|
||||
|
||||
static void StringOp(struct Machine *m, uint32_t rde, int op) {
|
||||
bool stop;
|
||||
void *p[2];
|
||||
unsigned n;
|
||||
uint64_t asz;
|
||||
bool compare;
|
||||
int64_t sgn, v;
|
||||
uint8_t s[3][8];
|
||||
sgn = GetFlag(m->flags, FLAGS_DF) ? -1 : 1;
|
||||
asz = Asz(rde) ? 0xffffffff : 0xffffffffffffffff;
|
||||
stop = false;
|
||||
n = 1 << RegLog2(rde);
|
||||
for (;;) {
|
||||
if (Rep(rde) && !Read64(m->cx)) break;
|
||||
v = 0;
|
||||
*p = NULL;
|
||||
compare = false;
|
||||
sgn = GetFlag(m->flags, FLAGS_DF) ? -1 : 1;
|
||||
do {
|
||||
if (Rep(rde) && !ReadCx(m, rde)) break;
|
||||
switch (op) {
|
||||
case STRING_CMPS:
|
||||
Alu(RegLog2(rde), ALU_SUB,
|
||||
ReadInt(Load(m, (Read64(m->si) + GetSegment(m)) & asz, n, s[2]),
|
||||
RegLog2(rde)),
|
||||
ReadInt(Load(m, Read64(m->di) & asz, n, s[1]), RegLog2(rde)),
|
||||
kAlu[ALU_SUB][RegLog2(rde)](
|
||||
ReadInt(Load(m, AddressSi(m, rde), n, s[2]), RegLog2(rde)),
|
||||
ReadInt(Load(m, AddressDi(m, rde), n, s[1]), RegLog2(rde)),
|
||||
&m->flags);
|
||||
Write64(m->di, (Read64(m->di) + sgn * n) & asz);
|
||||
Write64(m->si, (Read64(m->si) + sgn * n) & asz);
|
||||
compare = true;
|
||||
AddDi(m, rde, sgn * n);
|
||||
AddSi(m, rde, sgn * n);
|
||||
stop = (Rep(rde) == 2 && GetFlag(m->flags, FLAGS_ZF)) ||
|
||||
(Rep(rde) == 3 && !GetFlag(m->flags, FLAGS_ZF));
|
||||
break;
|
||||
case STRING_MOVS:
|
||||
memcpy(BeginStore(m, (v = Read64(m->di) & asz), n, p, s[0]),
|
||||
Load(m, (Read64(m->si) + GetSegment(m)) & asz, n, s[1]), n);
|
||||
Write64(m->di, (Read64(m->di) + sgn * n) & asz);
|
||||
Write64(m->si, (Read64(m->si) + sgn * n) & asz);
|
||||
memcpy(BeginStore(m, (v = AddressDi(m, rde)), n, p, s[0]),
|
||||
Load(m, AddressSi(m, rde), n, s[1]), n);
|
||||
AddDi(m, rde, sgn * n);
|
||||
AddSi(m, rde, sgn * n);
|
||||
EndStore(m, v, n, p, s[0]);
|
||||
break;
|
||||
case STRING_STOS:
|
||||
memcpy(BeginStore(m, (v = Read64(m->di) & asz), n, p, s[0]), m->ax, n);
|
||||
Write64(m->di, (Read64(m->di) + sgn * n) & asz);
|
||||
memcpy(BeginStore(m, (v = AddressDi(m, rde)), n, p, s[0]), m->ax, n);
|
||||
AddDi(m, rde, sgn * n);
|
||||
EndStore(m, v, n, p, s[0]);
|
||||
break;
|
||||
case STRING_LODS:
|
||||
memcpy(m->ax, Load(m, (Read64(m->si) + GetSegment(m)) & asz, n, s[1]),
|
||||
n);
|
||||
Write64(m->si, (Read64(m->si) + sgn * n) & asz);
|
||||
memcpy(m->ax, Load(m, AddressSi(m, rde), n, s[1]), n);
|
||||
AddSi(m, rde, sgn * n);
|
||||
break;
|
||||
case STRING_SCAS:
|
||||
Alu(RegLog2(rde), ALU_SUB,
|
||||
ReadInt(Load(m, Read64(m->di) & asz, n, s[1]), RegLog2(rde)),
|
||||
kAlu[ALU_SUB][RegLog2(rde)](
|
||||
ReadInt(Load(m, AddressDi(m, rde), n, s[1]), RegLog2(rde)),
|
||||
ReadInt(m->ax, RegLog2(rde)), &m->flags);
|
||||
Write64(m->di, (Read64(m->di) + sgn * n) & asz);
|
||||
compare = true;
|
||||
AddDi(m, rde, sgn * n);
|
||||
stop = (Rep(rde) == 2 && GetFlag(m->flags, FLAGS_ZF)) ||
|
||||
(Rep(rde) == 3 && !GetFlag(m->flags, FLAGS_ZF));
|
||||
break;
|
||||
case STRING_OUTS:
|
||||
OpOut(m, Read16(m->dx),
|
||||
ReadInt(Load(m, (Read64(m->si) + GetSegment(m)) & asz, n, s[1]),
|
||||
RegLog2(rde)));
|
||||
Write64(m->si, (Read64(m->si) + sgn * n) & asz);
|
||||
ReadInt(Load(m, AddressSi(m, rde), n, s[1]), RegLog2(rde)));
|
||||
AddSi(m, rde, sgn * n);
|
||||
break;
|
||||
case STRING_INS:
|
||||
WriteInt(BeginStore(m, (v = Read64(m->di) & asz), n, p, s[0]),
|
||||
WriteInt(BeginStore(m, (v = AddressDi(m, rde)), n, p, s[0]),
|
||||
OpIn(m, Read16(m->dx)), RegLog2(rde));
|
||||
Write64(m->di, (Read64(m->di) + sgn * n) & asz);
|
||||
AddDi(m, rde, sgn * n);
|
||||
EndStore(m, v, n, p, s[0]);
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
EndStore(m, v, n, p, s[0]);
|
||||
if (!Rep(rde)) break;
|
||||
Write64(m->cx, Read64(m->cx) - 1);
|
||||
if (compare) {
|
||||
if (Rep(rde) == 2 && GetFlag(m->flags, FLAGS_ZF)) break;
|
||||
if (Rep(rde) == 3 && !GetFlag(m->flags, FLAGS_ZF)) break;
|
||||
if (Rep(rde)) {
|
||||
SubtractCx(m, rde, 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (!stop);
|
||||
}
|
||||
|
||||
static void RepMovsbEnhanced(struct Machine *m, uint32_t rde) {
|
||||
uint8_t *direal, *sireal;
|
||||
uint64_t diactual, siactual, cx;
|
||||
unsigned diremain, siremain, i, n;
|
||||
if ((cx = ReadCx(m, rde))) {
|
||||
do {
|
||||
diactual = AddressDi(m, rde);
|
||||
siactual = AddressSi(m, rde);
|
||||
SetWriteAddr(m, diactual, cx);
|
||||
SetReadAddr(m, siactual, cx);
|
||||
direal = ResolveAddress(m, diactual);
|
||||
sireal = ResolveAddress(m, siactual);
|
||||
diremain = 0x1000 - (diactual & 0xfff);
|
||||
siremain = 0x1000 - (siactual & 0xfff);
|
||||
n = MIN(cx, MIN(diremain, siremain));
|
||||
if ((uintptr_t)direal <= (uintptr_t)sireal ||
|
||||
(uintptr_t)direal >= (uintptr_t)sireal + n) {
|
||||
memcpy(direal, sireal, n);
|
||||
} else {
|
||||
for (i = 0; i < n; ++i) {
|
||||
direal[i] = sireal[i];
|
||||
}
|
||||
}
|
||||
AddDi(m, rde, n);
|
||||
AddSi(m, rde, n);
|
||||
} while ((cx = SubtractCx(m, rde, n)));
|
||||
}
|
||||
}
|
||||
|
||||
void OpRepMovsbEnhanced(struct Machine *m, uint32_t rde) {
|
||||
bool failed;
|
||||
uint8_t *direal, *sireal;
|
||||
unsigned diremain, siremain, i, n;
|
||||
uint64_t divirtual, sivirtual, diactual, siactual, failaddr, asz, cx;
|
||||
if (!(cx = Read64(m->cx))) return;
|
||||
failed = false;
|
||||
failaddr = 0;
|
||||
asz = Asz(rde) ? 0xffffffff : 0xffffffffffffffff;
|
||||
divirtual = Read64(m->di) & asz;
|
||||
sivirtual = Read64(m->si) & asz;
|
||||
SetWriteAddr(m, (GetSegment(m) + divirtual) & asz, cx);
|
||||
SetReadAddr(m, (GetSegment(m) + sivirtual) & asz, cx);
|
||||
do {
|
||||
diactual = (GetSegment(m) + divirtual) & asz;
|
||||
siactual = (GetSegment(m) + sivirtual) & asz;
|
||||
if (!(direal = FindReal(m, diactual))) {
|
||||
failaddr = diactual;
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
if (!(sireal = FindReal(m, siactual))) {
|
||||
failaddr = siactual;
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
diremain = 0x1000 - (divirtual & 0xfff);
|
||||
siremain = 0x1000 - (sivirtual & 0xfff);
|
||||
n = MIN(cx, MIN(diremain, siremain));
|
||||
for (i = 0; i < n; ++i) {
|
||||
direal[i] = sireal[i];
|
||||
}
|
||||
cx -= n;
|
||||
divirtual = (divirtual + n) & asz;
|
||||
sivirtual = (sivirtual + n) & asz;
|
||||
} while (cx);
|
||||
Write64(m->cx, cx);
|
||||
Write64(m->di, divirtual);
|
||||
Write64(m->si, sivirtual);
|
||||
if (failed) ThrowSegmentationFault(m, failaddr);
|
||||
static void RepStosbEnhanced(struct Machine *m, uint32_t rde) {
|
||||
uint8_t *direal;
|
||||
unsigned diremain, n;
|
||||
uint64_t divirtual, diactual, cx;
|
||||
if ((cx = ReadCx(m, rde))) {
|
||||
do {
|
||||
diactual = AddressDi(m, rde);
|
||||
SetWriteAddr(m, diactual, cx);
|
||||
direal = ResolveAddress(m, diactual);
|
||||
diremain = 0x1000 - (diactual & 0xfff);
|
||||
n = MIN(cx, diremain);
|
||||
memset(direal, Read8(m->ax), n);
|
||||
AddDi(m, rde, n);
|
||||
} while ((cx = SubtractCx(m, rde, n)));
|
||||
}
|
||||
}
|
||||
|
||||
void OpRepStosbEnhanced(struct Machine *m, uint32_t rde) {
|
||||
bool failed;
|
||||
uint8_t *direal, al;
|
||||
unsigned diremain, i, n;
|
||||
uint64_t divirtual, diactual, failaddr, asz, cx;
|
||||
if (!(cx = Read64(m->cx))) return;
|
||||
failaddr = 0;
|
||||
failed = false;
|
||||
al = Read8(m->ax);
|
||||
asz = Asz(rde) ? 0xffffffff : 0xffffffffffffffff;
|
||||
divirtual = Read64(m->di) & asz;
|
||||
SetWriteAddr(m, (GetSegment(m) + divirtual) & asz, cx);
|
||||
do {
|
||||
diactual = (GetSegment(m) + divirtual) & asz;
|
||||
if (!(direal = FindReal(m, diactual))) {
|
||||
failaddr = diactual;
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
diremain = 0x1000 - (divirtual & 0xfff);
|
||||
n = MIN(cx, diremain);
|
||||
for (i = 0; i < n; ++i) {
|
||||
direal[i] = al;
|
||||
}
|
||||
cx -= n;
|
||||
divirtual = (divirtual + n) & asz;
|
||||
} while (cx);
|
||||
Write64(m->cx, cx);
|
||||
Write64(m->di, divirtual);
|
||||
if (failed) ThrowSegmentationFault(m, failaddr);
|
||||
void OpMovs(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_MOVS);
|
||||
}
|
||||
|
||||
void OpCmps(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_CMPS);
|
||||
}
|
||||
|
||||
void OpStos(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_STOS);
|
||||
}
|
||||
|
||||
void OpLods(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_LODS);
|
||||
}
|
||||
|
||||
void OpScas(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_SCAS);
|
||||
}
|
||||
|
||||
void OpIns(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_INS);
|
||||
}
|
||||
|
||||
void OpOuts(struct Machine *m, uint32_t rde) {
|
||||
StringOp(m, rde, STRING_OUTS);
|
||||
}
|
||||
|
||||
void OpMovsb(struct Machine *m, uint32_t rde) {
|
||||
if (Rep(rde) && !GetFlag(m->flags, FLAGS_DF)) {
|
||||
OpRepMovsbEnhanced(m, rde);
|
||||
RepMovsbEnhanced(m, rde);
|
||||
} else {
|
||||
OpString(m, rde, STRING_MOVS);
|
||||
OpMovs(m, rde);
|
||||
}
|
||||
}
|
||||
|
||||
void OpStosb(struct Machine *m, uint32_t rde) {
|
||||
if (Rep(rde) && !GetFlag(m->flags, FLAGS_DF)) {
|
||||
OpRepStosbEnhanced(m, rde);
|
||||
RepStosbEnhanced(m, rde);
|
||||
} else {
|
||||
OpString(m, rde, STRING_STOS);
|
||||
OpStos(m, rde);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue