Skip to content

Commit b17e581

Browse files
authored
Add support for Floatx80 new type (#28)
Add proper support for new `Floatx80` introduced in yrp604/bochscpu#12 in the `State` structure - without which segfault happened.
1 parent 8ebb16f commit b17e581

File tree

7 files changed

+66
-9
lines changed

7 files changed

+66
-9
lines changed

.github/build-bochscpu.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mkdir bxbuild
1212
cd bxbuild
1313

1414
REM Use WSL to configure / clone the repositories.
15-
bash -c "git clone https://github.com/yrp604/bochscpu-build.git && git clone https://github.com/yrp604/bochscpu.git && git clone https://github.com/yrp604/bochscpu-ffi.git && cd bochscpu-build && bash prep.sh && cd Bochs/bochs && bash .conf.cpu-msvc"
15+
bash -c "git clone https://github.com/yrp604/bochscpu-build.git && git clone https://github.com/hugsy/bochscpu.git && git clone https://github.com/yrp604/bochscpu-ffi.git && cd bochscpu-build && bash prep.sh && cd Bochs/bochs && bash .conf.cpu-msvc"
1616

1717
REM Build bochs; libinstrument.a is expected to fail to build so don't freak out.
1818
REM You can run nmake all-clean to clean up the build.

.github/build-bochscpu.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
Push-Location
4+
5+
New-Item -ItemType Directory -Name bxbuild
6+
Set-Location bxbuild
7+
8+
bash -c "
9+
git clone https://github.com/hugsy/bochscpu-build.git &&
10+
git clone https://github.com/hugsy/bochscpu.git &&
11+
git clone https://github.com/hugsy/bochscpu-ffi.git &&
12+
cd bochscpu-build && bash prep.sh && cd Bochs/bochs && bash .conf.cpu-msvc
13+
"
14+
15+
Set-Location bochscpu-build\Bochs\bochs
16+
$env:CL = "/MP"
17+
nmake
18+
19+
Remove-Item -Recurse -Force -ErrorAction Ignore ..\..\..\bochscpu\bochs
20+
Remove-Item -Recurse -Force -ErrorAction Ignore ..\..\..\bochscpu\lib
21+
22+
New-Item -ItemType Directory -Name ..\..\..\bochscpu\lib
23+
Copy-Item cpu\libcpu.a ..\..\..\bochscpu\lib\cpu.lib
24+
Copy-Item cpu\fpu\libfpu.a ..\..\..\bochscpu\lib\fpu.lib
25+
Copy-Item cpu\avx\libavx.a ..\..\..\bochscpu\lib\avx.lib
26+
Copy-Item cpu\cpudb\libcpudb.a ..\..\..\bochscpu\lib\cpudb.lib
27+
28+
New-Item -ItemType Directory -Name ..\..\..\bochscpu\bochs
29+
robocopy . ..\..\..\bochscpu\bochs /e
30+
31+
Set-Location ..\..\..\bochscpu-ffi
32+
cargo clean
33+
cargo build -j $env:NUMBER_OF_PROCESSORS
34+
cargo build -j $env:NUMBER_OF_PROCESSORS --release
35+
36+
Pop-Location

.github/build-bochscpu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mkdir bxbuild
1414
cd bxbuild
1515

1616
git clone https://github.com/yrp604/bochscpu-build.git
17-
git clone https://github.com/yrp604/bochscpu.git
17+
git clone https://github.com/hugsy/bochscpu.git
1818
git clone https://github.com/yrp604/bochscpu-ffi.git
1919

2020
cd bochscpu-build

.github/workflows/build.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
bindings:
8282
needs: bochscpu
8383
strategy:
84-
fail-fast: true
84+
fail-fast: false
8585
matrix:
8686
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
8787
variant:
@@ -152,6 +152,17 @@ jobs:
152152
cmake --install ./build --verbose --config ${{ matrix.variant.config }} --prefix ./artifact
153153
python -m pip wheel . -w ./wheel
154154
155+
- name: Install
156+
run: |
157+
python -m pip install . --user --upgrade
158+
python -m pip install capstone keystone-engine --user --upgrade
159+
160+
- name: Tests
161+
if: matrix.python-version == '3.12'
162+
run: |
163+
python examples/long_mode_fibonacci.py
164+
python examples/real_mode_print_hello_world.py --debug
165+
155166
- name: Upload artifacts
156167
uses: actions/upload-artifact@v4
157168
with:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# bochscpu-python
22

3-
[![python 3](https://img.shields.io/badge/python-3.8+-cyan)](https://python.org)
43
[![Python 3.8+](https://img.shields.io/pypi/v/bochscpu-python.svg)](https://pypi.org/project/bochscpu-python/)
54
[![Downloads](https://static.pepy.tech/badge/bochscpu-python)](https://pepy.tech/project/bochscpu-python)
65
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
76
[![Licence MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?maxAge=2592000?style=plastic)](https://github.com/hugsy/bochscpu-python/blob/main/LICENSE)
7+
[![Builds](https://github.com/hugsy/bochscpu-python/actions/workflows/build.yml/badge.svg)](https://github.com/hugsy/bochscpu-python/actions/workflows/build.yml)
88

99
Python bindings for [@yrp](https://github.com/yrp604/)'s [BochsCPU](https://github.com/yrp604/bochscpu) using [FFI](https://github.com/yrp604/bochscpu-ffi) to easily and accurately emulate x86 code.
1010

bochscpu/bochscpu.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ struct Zmm
131131
Zmm() = default;
132132
};
133133

134+
struct Floatx80
135+
{
136+
uint64_t fraction {};
137+
uint16_t exp {};
138+
139+
Floatx80() = default;
140+
};
141+
134142
struct State
135143
{
136144
uint64_t bochscpu_seed {};
@@ -179,7 +187,7 @@ struct State
179187
uint16_t fpsw {};
180188
uint16_t fptw {};
181189
uint16_t fpop {};
182-
std::array<uint64_t, 8> fpst {};
190+
std::array<Floatx80, 8> fpst {};
183191
uint32_t mxcsr {};
184192
uint32_t mxcsr_mask {};
185193
uint64_t tsc {};
@@ -207,6 +215,8 @@ using bochscpu_cpu_global_seg_t = GlobalSeg;
207215

208216
using bochscpu_cpu_zmm_t = Zmm;
209217

218+
using bochscpu_cpu_floatx80_t = Floatx80;
219+
210220
using bochscpu_instr_t = const void*;
211221

212222
extern "C"

python/inc/bochscpu.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ enum class InstructionType : uint32_t
108108
BOCHSCPU_INSTR_IS_SYSEXIT = BOCHSCPU_INSTR_IS_SYSEXIT,
109109
};
110110

111-
112111
enum class HookType : uint32_t
113112
{
114113
BOCHSCPU_HOOK_MEM_READ = BOCHSCPU_HOOK_MEM_READ,
@@ -126,7 +125,6 @@ enum class HookType : uint32_t
126125
BOCHSCPU_HOOK_TLB_INVPCID = BOCHSCPU_HOOK_TLB_INVPCID,
127126
};
128127

129-
130128
enum class OpcodeOperationType
131129
{
132130
BOCHSCPU_OPCODE_ERROR = BOCHSCPU_OPCODE_ERROR,
@@ -407,16 +405,18 @@ struct CPU
407405
this->__cpu = ::bochscpu_cpu_new(0);
408406
if ( !this->__cpu )
409407
throw std::runtime_error("Invalid CPU ID");
410-
dbg("Created CPU#%lu", this->id);
408+
dbg("Created CPU#%lu at %#x", this->id, this->__cpu);
411409
}
412410

413411
~CPU()
414412
{
413+
dbg("Destroying CPU#%lu at %#x", this->id, this->__cpu);
415414
::bochscpu_cpu_delete(this->__cpu);
415+
this->__cpu = nullptr;
416416
}
417417

418418
uint32_t id {0};
419-
bochscpu_cpu_t __cpu {};
419+
bochscpu_cpu_t __cpu {nullptr};
420420
};
421421
} // namespace Cpu
422422

0 commit comments

Comments
 (0)