[GH-ISSUE #23] Initial plan for cpu soft-virtualization #12

Closed
opened 2026-02-27 21:04:07 +03:00 by kerem · 19 comments
Owner

Originally created by @skmp on GitHub (Jul 11, 2023).
Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/23

Overview

The idea is to soft-virtualize, by reserving a few registers for the host system, and leaving everything else as is with static register allocation. RIP-relocations also need to be done.

  • Decode x86 instructions (core, rex, mmx/sse, and vex encodings)
  • Use a register as context and some scratch registers
  • Can use similar branch lookup structures as hex-emu does
  • RIP-relative accesses will have to be relocated
  • fs and gs segments need to be virtualized

Implementing using zydis

  • r14 as temp
  • r15 as context
  • free more temp regs as needed
  • RIP REL as double deref with rip moves

Kind of patches

  • RIP_REL
  • +r
  • MOD_r
  • MOD_m
  • SIB_b
  • SIB_i

x2 for rex versions

  • vex encodings

Notes

Windows TLS doesn't give direct access to TLS slots in the same way linux TLS does, so we'll definitely need a context register.

Original writeup #### Vasika - Elafria eikoniki mixani pou doulevei se leitourgia xristi gia x86/64 arxitetoniki ipodoxis - Vasismeno stous metapinakes apo to hex - Gennitiki arxitekoniki me metaprogramatismo - Eikonefsi ton kataxoriton me statiki antistixisi gia tous perisoterous, mono oti spanizei se dinamiki katanomi - Eikonefsi diefthiseon ektelesis meson paromion domon antistixisis me to hex

Epipleon

  • Eikonefsi entolon SSE4a pou isos spanizoun

Ektimiseis

  • ~ 5-15K grammes gia ton pirina + metapinakes
  • me xrisi mono metafraseon gramikis ektelesis, ~ 10% fira sta almata rois
  • me kapia epipleon poliplokotita kai xrisi diagramaton rois elenxou/ektelesis, < 2% fira?, alla isos kai travlisma kata tin eikonopiisi tis filoksenoumenis arxitektonikis

Xronika

  • ~ 1w gia PoC
  • ~ + 2w gia aploki, gramiki ektelesi
  • ~ + 4w gia DRE me polapla simia eisodou

Pithano sxedio ergasion

  • Aploiki ilopiisi me ton fortoti ksotikon tou hex + metapinakes + ola apla
  • Anaparagontopiisi meta gia artiotita kai aisthitiki kodika
  • Sindesi me ton fortoti ksotikon gia to PS4

Sxolia

  • @georgemoralis ti les, pos s fenete?
  • hex-emu gia reference
Originally created by @skmp on GitHub (Jul 11, 2023). Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/23 ### Overview The idea is to soft-virtualize, by reserving a few registers for the host system, and leaving everything else as is with static register allocation. RIP-relocations also need to be done. - Decode x86 instructions (core, rex, mmx/sse, and vex encodings) - Use a register as context and some scratch registers - Can use similar branch lookup structures as hex-emu does - RIP-relative accesses will have to be relocated - fs and gs segments need to be virtualized #### Implementing using zydis - r14 as temp - r15 as context - free more temp regs as needed - RIP REL as double deref with rip moves #### Kind of patches - RIP_REL - +r - MOD_r - MOD_m - SIB_b - SIB_i x2 for rex versions + vex encodings #### Notes Windows TLS doesn't give direct access to TLS slots in the same way linux TLS does, so we'll definitely need a context register. <details> <summary> Original writeup </summary> #### Vasika - Elafria eikoniki mixani pou doulevei se leitourgia xristi gia x86/64 arxitetoniki ipodoxis - Vasismeno stous metapinakes apo to hex - Gennitiki arxitekoniki me metaprogramatismo - Eikonefsi ton kataxoriton me statiki antistixisi gia tous perisoterous, mono oti spanizei se dinamiki katanomi - Eikonefsi diefthiseon ektelesis meson paromion domon antistixisis me to hex #### Epipleon - Eikonefsi entolon SSE4a pou isos spanizoun #### Ektimiseis - ~ 5-15K grammes gia ton pirina + metapinakes - me xrisi mono metafraseon gramikis ektelesis, ~ 10% fira sta almata rois - me kapia epipleon poliplokotita kai xrisi diagramaton rois elenxou/ektelesis, < 2% fira?, alla isos kai travlisma kata tin eikonopiisi tis filoksenoumenis arxitektonikis #### Xronika - ~ 1w gia PoC - ~ + 2w gia aploki, gramiki ektelesi - ~ + 4w gia DRE me polapla simia eisodou ### Pithano sxedio ergasion - [ ] Aploiki ilopiisi me ton fortoti ksotikon tou hex + metapinakes + ola apla - [ ] Anaparagontopiisi meta gia artiotita kai aisthitiki kodika - [ ] Sindesi me ton fortoti ksotikon gia to PS4 ### Sxolia - @georgemoralis ti les, pos s fenete? - [hex-emu](https://gitlab.com/hex-emu/hex-emu) gia reference </details>
kerem closed this issue 2026-02-27 21:04:07 +03:00
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

καλό φαινεται προχωρα :D

<!-- gh-comment-id:1630946030 --> @georgemoralis commented on GitHub (Jul 11, 2023): καλό φαινεται προχωρα :D
Author
Owner

@wheremyfoodat commented on GitHub (Jul 11, 2023):

Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar?

<!-- gh-comment-id:1631092654 --> @wheremyfoodat commented on GitHub (Jul 11, 2023): Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar?
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

Δεν νομιζω οτι έχει AVX2 μεχρι AVX φτανει

<!-- gh-comment-id:1631101535 --> @georgemoralis commented on GitHub (Jul 11, 2023): Δεν νομιζω οτι έχει AVX2 μεχρι AVX φτανει
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

MMX instructions
SSE / Streaming SIMD Extensions
SSE2 / Streaming SIMD Extensions 2
SSE3 / Streaming SIMD Extensions 3
SSSE3 / Supplemental Streaming SIMD Extensions 3
SSE4a ?
SSE4 / SSE4.1 + SSE4.2 / Streaming SIMD Extensions 4 ?
AES / Advanced Encryption Standard instructions
AVX / Advanced Vector Extensions
BMI1 / Bit Manipulation instructions 1
F16C / 16-bit Floating-Point conversion instructions
AMD64 / AMD 64-bit technology ?
AMD-V / AMD Virtualization technology

<!-- gh-comment-id:1631106452 --> @georgemoralis commented on GitHub (Jul 11, 2023): MMX instructions SSE / Streaming SIMD Extensions SSE2 / Streaming SIMD Extensions 2 SSE3 / Streaming SIMD Extensions 3 SSSE3 / Supplemental Streaming SIMD Extensions 3 SSE4a [ ? ](https://www.cpu-world.com/Glossary/S/SSE4a.html) SSE4 / SSE4.1 + SSE4.2 / Streaming SIMD Extensions 4 [ ? ](https://www.cpu-world.com/Glossary/S/SSE4.html) AES / Advanced Encryption Standard instructions AVX / Advanced Vector Extensions BMI1 / Bit Manipulation instructions 1 F16C / 16-bit Floating-Point conversion instructions AMD64 / AMD 64-bit technology [ ? ](https://www.cpu-world.com/Glossary/A/AMD64_technology.html) AMD-V / AMD Virtualization technology
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

οποτε στόχος επεξεργαστής? τι λιγοτερο μπορουμε να παρουμε...

<!-- gh-comment-id:1631107598 --> @georgemoralis commented on GitHub (Jul 11, 2023): οποτε στόχος επεξεργαστής? τι λιγοτερο μπορουμε να παρουμε...
Author
Owner

@wheremyfoodat commented on GitHub (Jul 11, 2023):

Βαλε το xbyak να τσεκάρει οτι εχεις AVX και F16C. Αυτό πρέπει να καλύπτει ολα τα Intel chips από Ivy Bridge και μετα.

Τo AMD-V δεν μας ενδιαφέρει γιατί αποκλείεται να δεις userspace code να φτιαχνει VM.

<!-- gh-comment-id:1631112304 --> @wheremyfoodat commented on GitHub (Jul 11, 2023): Βαλε το xbyak να τσεκάρει οτι εχεις AVX και F16C. Αυτό πρέπει να καλύπτει ολα τα Intel chips από Ivy Bridge και μετα. Τo AMD-V δεν μας ενδιαφέρει γιατί αποκλείεται να δεις userspace code να φτιαχνει VM.
Author
Owner

@wheremyfoodat commented on GitHub (Jul 11, 2023):

Λεω Xbyak επειδη 99% θα καταληξουμε να το εχουμε καπου στο repo και εχει functions που κανουν CPUID τυπου github.com/wheremyfoodat/Panda3DS@27cf8d75ca/include/PICA/dynapica/shader_rec_emitter_x64.hpp (L114-L125)

<!-- gh-comment-id:1631116655 --> @wheremyfoodat commented on GitHub (Jul 11, 2023): Λεω Xbyak επειδη 99% θα καταληξουμε να το εχουμε καπου στο repo και εχει functions που κανουν CPUID τυπου https://github.com/wheremyfoodat/Panda3DS/blob/27cf8d75caf3a685e56a5e88d314b679025c1154/include/PICA/dynapica/shader_rec_emitter_x64.hpp#L114-L125
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

ναι οπως ελεγα στον @skmp χρειαζεται για να patcharo καποια pragmata οπως το TLS

<!-- gh-comment-id:1631117993 --> @georgemoralis commented on GitHub (Jul 11, 2023): ναι οπως ελεγα στον @skmp χρειαζεται για να patcharo καποια pragmata οπως το TLS
Author
Owner

@wheremyfoodat commented on GitHub (Jul 11, 2023):

TLS == thread-local storage?

<!-- gh-comment-id:1631118898 --> @wheremyfoodat commented on GitHub (Jul 11, 2023): TLS == thread-local storage?
Author
Owner

@georgemoralis commented on GitHub (Jul 11, 2023):

ya

<!-- gh-comment-id:1631119465 --> @georgemoralis commented on GitHub (Jul 11, 2023): ya
Author
Owner

@skmp commented on GitHub (Jul 11, 2023):

Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar?

afto thelei pio plires emulation, opote dunno. me to backend to hex, isos, kathos k arm64 for sure.

<!-- gh-comment-id:1631184979 --> @skmp commented on GitHub (Jul 11, 2023): > Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar? afto thelei pio plires emulation, opote dunno. me to backend to hex, isos, kathos k arm64 for sure.
Author
Owner

@skmp commented on GitHub (Jul 11, 2023):

(updated ticket with hex reference)

@wheremyfoodat https://www.youtube.com/watch?v=ZYN-VqnvWmI gia perf numbers, an kai den exei pesei kapio optimisation effort

<!-- gh-comment-id:1631191849 --> @skmp commented on GitHub (Jul 11, 2023): (updated ticket with hex reference) @wheremyfoodat https://www.youtube.com/watch?v=ZYN-VqnvWmI gia perf numbers, an kai den exei pesei kapio optimisation effort
Author
Owner

@georgemoralis commented on GitHub (Jul 12, 2023):

Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar?

afto thelei pio plires emulation, opote dunno. me to backend to hex, isos, kathos k arm64 for sure.

ps4 se arm64? gamato!

<!-- gh-comment-id:1632183657 --> @georgemoralis commented on GitHub (Jul 12, 2023): > > Skopeuoume na ypostiriksoyme panarxaious epeksergastes typou xoris AVX2? H mono osoys einai pio kainourgioi apo to Jaguar? > > afto thelei pio plires emulation, opote dunno. me to backend to hex, isos, kathos k arm64 for sure. ps4 se arm64? gamato!
Author
Owner

@skmp commented on GitHub (Jul 12, 2023):

me vulkan renderer standard pezei k android :3

<!-- gh-comment-id:1632357604 --> @skmp commented on GitHub (Jul 12, 2023): me vulkan renderer standard pezei k android :3
Author
Owner

@georgemoralis commented on GitHub (Jul 12, 2023):

vulkan einai to plano

<!-- gh-comment-id:1632358505 --> @georgemoralis commented on GitHub (Jul 12, 2023): vulkan einai to plano
Author
Owner

@skmp commented on GitHub (Aug 9, 2023):

arxiki idea ~ https://github.com/skmp/vex86/blob/main/main.cpp.

Prepei episeis relocation ta rip relative sections, ektos apo ta branches.

<!-- gh-comment-id:1672300909 --> @skmp commented on GitHub (Aug 9, 2023): arxiki idea ~ https://github.com/skmp/vex86/blob/main/main.cpp. Prepei episeis relocation ta rip relative sections, ektos apo ta branches.
Author
Owner

@skmp commented on GitHub (Aug 9, 2023):

to opio distixos thelei ena temp hrfm :|

<!-- gh-comment-id:1672306724 --> @skmp commented on GitHub (Aug 9, 2023): to opio distixos thelei ena temp hrfm :|
Author
Owner

@skmp commented on GitHub (Aug 9, 2023):

TLS to the rescue?!

<!-- gh-comment-id:1672312209 --> @skmp commented on GitHub (Aug 9, 2023): TLS to the rescue?!
Author
Owner

@georgemoralis commented on GitHub (Jun 5, 2024):

close until we need it again

<!-- gh-comment-id:2150258995 --> @georgemoralis commented on GitHub (Jun 5, 2024): close until we need it again
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#12
No description provided.