1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 00:55:08 +00:00
Commit graph

105 commits

Author SHA1 Message Date
Andreas Kling
e4b068aec5 UserspaceEmulator: Fix buggy IDIV instructions
These were not doing mashing together the signed double-size results
correctly and lost bits in the signed/unsigned casting process.
2020-07-18 17:57:40 +02:00
Andreas Kling
9e6d002660 UserspaceEmulator: Fix buggy IMUL instructions
These were not recording the higher part of the result correctly.
Since the flags are much less complicated than the inline assembly
here, just implement IMUL in C++ instead.
2020-07-18 17:57:40 +02:00
Andreas Kling
02882d5345 UserspaceEmulator: Add single-operand MUL and DIV instructions
These are the unsigned variants. Signed variants sold separately.
2020-07-18 17:57:40 +02:00
Andreas Kling
30d512144e UserspaceEmulator: Implement the BSF and BSF instructions
BSF maps nicely to __builtin_ctz(), but for BSR we have to bust out
some inline assembly to get exactly what we want.
2020-07-18 17:57:40 +02:00
Andreas Kling
becbf36711 UserspaceEmulator: Fix XCHG_AX_reg16 overwriting entire EAX
This instruction should only write to the lower 16 bits (AX)
2020-07-18 00:25:02 +02:00
Andreas Kling
8959f9950a UserspaceEmulator: Simplify the STOSB/STOSW/STOSD instructions 2020-07-18 00:25:02 +02:00
Andreas Kling
79290696cf UserspaceEmulator: Simplify MOVSB/MOVSW/MOVSD instructions
Use the new loop instruction helpers.
2020-07-18 00:25:02 +02:00
Andreas Kling
f70f530722 UserspaceEmulator: Implement the SCASB/SCASW/SCASD instructions 2020-07-18 00:25:02 +02:00
Andreas Kling
41bbedc41d UserspaceEmulator: Implement the LODSB/LODSW/LODSD instructions
Look how nice they look with the new loop instruction helpers. :^)
2020-07-18 00:25:02 +02:00
Andreas Kling
c3441719ea UserspaceEmulator: Implement the JCXZ instruction 2020-07-18 00:25:02 +02:00
Andreas Kling
d321dc0a74 UserspaceEmulator: Fix too-wide accumulator used in 8/16 bit CMPXCHG 2020-07-18 00:25:02 +02:00
Andreas Kling
485d1faf09 UserspaceEmulator: Add helpers for making loop instructions generic
Use them to implement CMPSB/CMPSW/CMPSD.
2020-07-18 00:25:02 +02:00
Andreas Kling
28b6ba56aa UserspaceEmulator: Add the LOOP/LOOPZ/LOOPNZ instructions 2020-07-18 00:25:02 +02:00
Andreas Kling
af7a1eca0b UserspaceEmulator: Implement the XLAT instruction :^) 2020-07-18 00:25:02 +02:00
Andreas Kling
86a7820ad7 UserspaceEmulator: Add 16-bit PUSH/POP instructions 2020-07-18 00:25:02 +02:00
Andreas Kling
d153fbf44e UserspaceEmulator: Implement the BT/BTS/BTR/BTC instruction set 2020-07-18 00:25:02 +02:00
Andreas Kling
06669f3f0f UserspaceEmulator: Implement IMUL_RM8 and IMUL_RM32
These are both a little tricky since they produce a result wider than
the inputs.
2020-07-18 00:25:02 +02:00
Andreas Kling
9f1221c785 UserspaceEmulator: Implement the ROL/ROR/RCL/RCR instructions 2020-07-16 19:21:45 +02:00
Andreas Kling
897af8b4f7 UserspaceEmulator: Implement more SHLD/SHRD variants 2020-07-16 19:21:45 +02:00
Andreas Kling
db1929e3ff UserspaceEmulator: Make the shift/rotate instructions more generic 2020-07-16 19:21:45 +02:00
Andreas Kling
acfae91032 UserspaceEmulator: Fix incorrect SALC behavior
As @tzoz pointed out, SALC should set AL to 0xff when CF=1, not 0x01.

Fixes #2819.
2020-07-16 00:50:55 +02:00
Andreas Kling
c314292319 UserspaceEmulator: Catch use-after-frees by tracking malloc/free :^)
This patch introduces a "MallocTracer" to the UserspaceEmulator.
If this object is present on the Emulator, it can be notified whenever
the emulated program does a malloc() or free().

The notifications come in via a magic instruction sequence that we
embed in the LibC malloc() and free() functions. The sequence is:

    "salc x2, push reg32 x2, pop reg32 x3"

The data about the malloc/free operation is in the three pushes.
We make sure the sequence is harmless when running natively.

Memory accesses on MmapRegion are then audited to see if they fall
inside a known-to-be-freed malloc chunk. If so, we complain loud
and red in the debugger output. :^)

This is very, very cool! :^)

It's also a whole lot slower than before, since now we're auditing
memory accesses against a new set of metadata. This will need to be
optimized (and running in this mode should be opt-in, perhaps even
a separate program, etc.)
2020-07-15 23:25:20 +02:00
Andreas Kling
feebe3f42e UserspaceEmulator: Add partial support for the SHLD/SHRD instructions
We don't support all the addressing modes yet, but it won't be very
hard to add the rest of them when needed.
2020-07-15 18:47:45 +02:00
Andreas Kling
0ce4d3e942 UserspaceEmulator: Dump backtrace on FPU instruction 2020-07-15 18:47:45 +02:00
Andreas Kling
76b2a2789b UserspaceEmulator: Implement MUL_RM32 2020-07-15 18:47:45 +02:00
Andreas Kling
029fe56d69 UserspaceEmulator: Implement the 32-bit BSWAP instruction :^) 2020-07-15 18:47:45 +02:00
Andreas Kling
0781868092 UserspaceEmulator: Implement IDIV_RM32 2020-07-15 13:42:15 +02:00
Andreas Kling
400a252720 UserspaceEmulator: Implement the CBW/CDQ/CWD/CWDE instructions 2020-07-15 13:42:15 +02:00
Andreas Kling
6a926a8c61 LibX86+UserspaceEmulator: Don't store a32 in MemoryOrRegisterReference
The a32 bit tells us whether a memory address is 32-bit or not.
We already have this information in Instruction, so just plumb that
around instead of double-caching the bit.
2020-07-15 13:42:15 +02:00
Andreas Kling
f608b9d89a UserspaceEmulator: Mark some generic instructions ALWAYS_INLINE :^) 2020-07-13 20:47:45 +02:00
Andreas Kling
2f81c20002 UserspaceEmulator: Move the SoftCPU stream virtuals to the header
They don't actually get inlined yet, but at least this devirtualizes
them which is nice.
2020-07-13 20:41:48 +02:00
Andreas Kling
a27473cbc2 UserspaceEmulator+LibX86: Turn on -O3 optimization for emulation code
Since this code is performance-sensitive, let's have the compiler do
whatever it can to help us with the most important files.

This yields a ~8% speedup.
2020-07-13 20:23:00 +02:00
Andreas Kling
8656835935 UserspaceEmulator: Add a very simple instruction fetch cache
To avoid MMU region lookup on every single instruction fetch, we now
cache a raw pointer to the current instruction. This gets automatically
invalidated when we jump somewhere, but as long as we're executing
sequentially, instruction fetches will hit the cache and bypass all
the region lookup stuff.

This is about a ~2x speedup. :^)
2020-07-13 20:14:14 +02:00
Andreas Kling
a83fe7f82d UserspaceEmulator: Add the POPFD instruction
I'm not sure the mask I'm using here is completely correct, but it's
not terribly important since we're a userspace-only emulator anyway.
2020-07-13 13:50:22 +02:00
Andreas Kling
dba6f9b24b UserspaceEmulator: Add the NOT instruction (with bonus: NOP!) 2020-07-13 13:50:22 +02:00
Andreas Kling
5ecbfd8451 UserspaceEmulator: Add the STC/CLC and STD/CLD instructions 2020-07-13 13:50:22 +02:00
Andreas Kling
9f293054e8 UserspaceEmulator: Implement the ADC instruction 2020-07-13 13:50:22 +02:00
Andreas Kling
6230c60296 UserspaceEmulator: Make SBB actually respect the SoftCPU carry flag
We were forgetting to set the host CPU's carry flag before executing
the SBB instruction. This made the result a bit unpredictable. :^)
2020-07-13 13:50:22 +02:00
Andreas Kling
a9f92e5d75 UserspaceEmulator: Remove an unnecessary step in some instructions
We don't need to move the result of shifts around like this, we can
just use inline assembly outputs to make it end up in the right place.
2020-07-13 13:50:22 +02:00
Andreas Kling
80d3306840 UserspaceEmulator: Fix wrong ESI/EDI step sizes in MOVSW and MOVSD 2020-07-13 13:50:22 +02:00
Andreas Kling
63d3f5d19b UserspaceEmulator: Implement the PUSHFD instruction 2020-07-13 13:50:22 +02:00
Andreas Kling
079021a607 UserspaceEmulator: Put the executable name in argv[0] :^)
The emulated program can now find its own name in argv[0]. Very cool!
2020-07-12 21:37:54 +02:00
Andreas Kling
e461e3c8b0 UserspaceEmulator: Fix missing sign extension in PUSH_imm8 2020-07-12 17:44:14 +02:00
Andreas Kling
274ac3c628 UserspaceEmulator: Implement the XADD instruction 2020-07-12 15:35:01 +02:00
Andreas Kling
04695957e2 UserspaceEmulator: Implement the MOVSX instruction 2020-07-12 15:33:29 +02:00
Andreas Kling
8940916232 UserspaceEmulator: Implement JMP_RM32 2020-07-12 14:54:30 +02:00
Andreas Kling
a424208399 UserspaceEmulator: Implement DIV_RM32
Not using inline assembly for this one since flags are undefined after
a DIV instruction anyway.
2020-07-12 14:53:19 +02:00
Andreas Kling
062e2f8614 UserspaceEmulator: Implement the XCHG instruction 2020-07-12 14:45:46 +02:00
Andreas Kling
536ca0f8c9 UserspaceEmulator: Implement some more MOV variants 2020-07-12 14:45:35 +02:00
Andreas Kling
2d44f4526a UserspaceEmulator: Implement MOVSB/MOVSW/MOVSD 2020-07-12 14:45:02 +02:00