Skip to content

Commit

Permalink
Add HPPA(PA-RISC) architecture (capstone-engine#2265)
Browse files Browse the repository at this point in the history
* Refactor HPPA

* Add full HPPA 1.1 instructions support

* Add HPPA 1.1 cs tests

* Fix HPPA dissassembler

* Add HPPA 2.0 instructions

* Add HPPA tests

* Fix HPPA disasm & printer

* Update HPPA tests

* Remove unused code

* Add implicit register access info & Refactor HPPA main files

* Add python bindings/tests and cstests

* Fix HPPA disasm wrong decoding

* Rewrite invalid test cases

* Update HPPA python constants

* Make HPPA python test executable

* Change HPPA python tests sequence to match c tests

* Refactor HPPA main files

* Write target instead of offset in details

* Add HPPA detail function support in cstest

* Rewrite targets in branch tests

* Make correct string modifier addition

* Add hppa test calls

* Add zero operands check

* Remove MCOperand array

* Change immediate values printing

* Add HPPA 2.0 wide support

* Fix invalid break instruction decode

Remove unused code

* Add HPPA to fuzzing tests

* Add HPPA to options

* Add HPPA to docs

* Refactor HPPA

* Fix invalid branch insn decoding

* Add HPPA to labeler

* clang-format hppa files

* Document internal structures and minor refactoring

* Add missing default statements

* Fix invalid default statement
  • Loading branch information
R33v0LT authored Mar 26, 2024
1 parent 0d12bca commit 9daa1ff
Show file tree
Hide file tree
Showing 79 changed files with 8,639 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ EVM:
- include/capstone/evm.h
- tests/test_evm.c

HPPA:
- arch/HPPA/*
- cstool/cstool_hppa.c
- include/capstone/hppa.h
- suite/MC/HPPA/*
- tests/test_hppa.c

M680X:
- arch/M680X/*
- cstool/cstool_m680x.c
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ tests/test_bpf
tests/test_sh
tests/test_riscv
tests/test_sh
tests/test_alpha
tests/test_hppa

# regress binaries
suite/regress/invalid_read_in_print_operand
Expand Down
27 changes: 25 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ if(APPLE AND NOT CAPSTONE_BUILD_MACOS_THIN)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
endif()

set(SUPPORTED_ARCHITECTURES ARM AARCH64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE ALPHA)
set(SUPPORTED_ARCHITECTURE_LABELS ARM AARCH64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore Alpha)
set(SUPPORTED_ARCHITECTURES ARM AARCH64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE ALPHA HPPA)
set(SUPPORTED_ARCHITECTURE_LABELS ARM AARCH64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore Alpha HPPA)

list(LENGTH SUPPORTED_ARCHITECTURES count)
math(EXPR count "${count}-1")
Expand Down Expand Up @@ -169,6 +169,7 @@ set(HEADERS_COMMON
include/capstone/platform.h
include/capstone/sh.h
include/capstone/alpha.h
include/capstone/hppa.h
)

set(TEST_SOURCES test_basic.c test_detail.c test_skipdata.c test_iter.c)
Expand Down Expand Up @@ -617,6 +618,24 @@ if (CAPSTONE_ALPHA_SUPPORT)
set(TEST_SOURCES ${TEST_SOURCES} test_alpha.c)
endif ()

if(CAPSTONE_HPPA_SUPPORT)
add_definitions(-DCAPSTONE_HAS_HPPA)
set(SOURCES_HPPA
arch/HPPA/HPPADisassembler.c
arch/HPPA/HPPAInstPrinter.c
arch/HPPA/HPPAMapping.c
arch/HPPA/HPPAModule.c
)
set(HEADERS_HPPA
arch/HPPA/HPPAConstants.h
arch/HPPA/HPPADisassembler.h
arch/HPPA/HPPAInstPrinter.h
arch/HPPA/HPPAMapping.h
arch/HPPA/HPPAModule.h
)
set(TEST_SOURCES ${TEST_SOURCES} test_hppa.c)
endif()

if (CAPSTONE_OSXKERNEL_SUPPORT)
add_definitions(-DCAPSTONE_HAS_OSXKERNEL)
endif()
Expand All @@ -642,6 +661,7 @@ set(ALL_SOURCES
${SOURCES_SH}
${SOURCES_TRICORE}
${SOURCES_ALPHA}
${SOURCES_HPPA}
)

set(ALL_HEADERS
Expand All @@ -666,6 +686,7 @@ set(ALL_HEADERS
${HEADERS_SH}
${HEADERS_TRICORE}
${HEADERS_ALPHA}
${HEADERS_HPPA}
)

## properties
Expand Down Expand Up @@ -730,6 +751,7 @@ source_group("Source\\RISCV" FILES ${SOURCES_RISCV})
source_group("Source\\SH" FILES ${SOURCES_SH})
source_group("Source\\TriCore" FILES ${SOURCES_TRICORE})
source_group("Source\\Alpha" FILES ${SOURCES_ALPHA})
source_group("Source\\HPPA" FILES ${SOURCES_HPPA})

source_group("Include\\Common" FILES ${HEADERS_COMMON})
source_group("Include\\Engine" FILES ${HEADERS_ENGINE})
Expand All @@ -752,6 +774,7 @@ source_group("Include\\RISCV" FILES ${HEADERS_RISCV})
source_group("Include\\SH" FILES ${HEADERS_SH})
source_group("Include\\TriCore" FILES ${HEADERS_TRICORE})
source_group("Include\\Alpha" FILES ${HEADERS_ALPHA})
source_group("Include\\HPPA" FILES ${HEADERS_HPPA})

## installation
if(CAPSTONE_INSTALL)
Expand Down
1 change: 1 addition & 0 deletions COMPILE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
/usr/include/capstone/bpf.h
/usr/include/capstone/capstone.h
/usr/include/capstone/evm.h
/usr/include/capstone/hppa.h
/usr/include/capstone/m680x.h
/usr/include/capstone/m68k.h
/usr/include/capstone/mips.h
Expand Down
3 changes: 2 additions & 1 deletion COMPILE_CMAKE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Get CMake for free from http://www.cmake.org.
- CAPSTONE_ARM_SUPPORT: support ARM. Run cmake with -DCAPSTONE_ARM_SUPPORT=0 to remove ARM.
- CAPSTONE_AARCH64_SUPPORT: support AARCH64. Run cmake with -DCAPSTONE_AARCH64_SUPPORT=0 to remove AARCH64.
- CAPSTONE_ALPHA_SUPPORT: support Alpha. Run cmake with -DCAPSTONE_ALPHA_SUPPORT=0 to remove Alpha.
- CAPSTONE_HPPA_SUPPORT: support HPPA. Run cmake with -DCAPSTONE_HPPA_SUPPORT=0 to remove HPPA.
- CAPSTONE_M680X_SUPPORT: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X.
- CAPSTONE_M68K_SUPPORT: support M68K. Run cmake with -DCAPSTONE_M68K_SUPPORT=0 to remove M68K.
- CAPSTONE_MIPS_SUPPORT: support Mips. Run cmake with -DCAPSTONE_MIPS_SUPPORT=0 to remove Mips.
Expand Down Expand Up @@ -117,7 +118,7 @@ Get CMake for free from http://www.cmake.org.

Will just target the x86 architecture. The list of available architectures is: ARM,
AARCH64, M68K, MIPS, PowerPC, Sparc, SystemZ, XCore, x86, TMS320C64x, M680x, EVM, MOS65XX,
WASM, BPF, RISCV, Alpha.
WASM, BPF, RISCV, Alpha, HPPA.

(4) You can also create an installation image with cmake, by using the 'install' target.
Use:
Expand Down
1 change: 1 addition & 0 deletions COMPILE_MSVC.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ or newer versions are required.
- CAPSTONE_HAS_ARM: support ARM. Delete this to remove ARM support.
- CAPSTONE_HAS_AARCH64: support AARCH64. Delete this to remove AARCH64 support.
- CAPSTONE_HAS_ALPHA: support Alpha. Delete this to remove Alpha support.
- CAPSTONE_HAS_HPPA: support HPPA. Delete this to remove HPPA support.
- CAPSTONE_HAS_M68K: support M68K. Delete this to remove M68K support.
- CAPSTONE_HAS_MIPS: support Mips. Delete this to remove Mips support.
- CAPSTONE_HAS_POWERPC: support PPC. Delete this to remove PPC support.
Expand Down
2 changes: 1 addition & 1 deletion CREDITS.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ fanfuqiang & citypw & porto703 : RISCV architecture.
Josh "blacktop" Maine: Arm64 architecture improvements.
Finn Wilkinson: AArch64 update to Armv9.2-a (SME + SVE2 support)
Billow & Sidneyp : TriCore architecture.
Dmitry Sibirtsev: Alpha architecture.
Dmitry Sibirtsev: Alpha & HPPA architecture.
1 change: 1 addition & 0 deletions HACK.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Capstone source is organized as followings.
│   ├── ARM <- ARM engine
│   ├── BPF <- Berkeley Packet Filter engine
│   ├── EVM <- Ethereum engine
│   ├── HPPA <- HPPA engine
│   ├── M680X <- M680X engine
│   ├── M68K <- M68K engine
│   ├── Mips <- Mips engine
Expand Down
1 change: 1 addition & 0 deletions MCInst.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void MCInst_Init(MCInst *inst)
inst->tied_op_idx[i] = -1;
inst->isAliasInstr = false;
inst->fillDetailOps = false;
memset(&inst->hppa_ext, 0, sizeof(inst->hppa_ext));
}

void MCInst_clear(MCInst *inst)
Expand Down
1 change: 1 addition & 0 deletions MCInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ struct MCInst {
uint8_t xAcquireRelease; // X86 xacquire/xrelease
bool isAliasInstr; // Flag if this MCInst is an alias.
bool fillDetailOps; // If set, detail->operands gets filled.
hppa_ext hppa_ext; ///< for HPPA operand. Contains info about modifiers and their effect on the instruction
};

void MCInst_Init(MCInst *inst);
Expand Down
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,22 @@ ifneq (,$(findstring alpha,$(CAPSTONE_ARCHS)))
LIBOBJ_ALPHA += $(LIBSRC_ALPHA:%.c=$(OBJDIR)/%.o)
endif

DEP_HPPA =
DEP_HPPA += $(wildcard arch/HPPA/HPPA*.inc)

LIBOBJ_HPPA =
ifneq (,$(findstring hppa,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_HPPA
LIBSRC_HPPA += $(wildcard arch/HPPA/HPPA*.c)
LIBOBJ_HPPA += $(LIBSRC_HPPA:%.c=$(OBJDIR)/%.o)
endif


LIBOBJ =
LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o $(OBJDIR)/MCInst.o $(OBJDIR)/MCInstPrinter.o $(OBJDIR)/Mapping.o
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_AARCH64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_RISCV) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_SH)
LIBOBJ += $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM) $(LIBOBJ_MOS65XX) $(LIBOBJ_WASM) $(LIBOBJ_BPF)
LIBOBJ += $(LIBOBJ_TRICORE) $(LIBOBJ_ALPHA)
LIBOBJ += $(LIBOBJ_TRICORE) $(LIBOBJ_ALPHA) $(LIBOBJ_HPPA)


ifeq ($(PKG_EXTRA),)
Expand Down Expand Up @@ -476,6 +487,7 @@ $(LIBOBJ_MOS65XX): $(DEP_MOS65XX)
$(LIBOBJ_BPF): $(DEP_BPF)
$(LIBOBJ_TRICORE): $(DEP_TRICORE)
$(LIBOBJ_ALPHA): $(DEP_ALPHA)
$(LIBOBJ_HPPA): $(DEP_HPPA)

ifeq ($(CAPSTONE_STATIC),yes)
$(ARCHIVE): $(LIBOBJ)
Expand Down Expand Up @@ -561,13 +573,13 @@ dist:
git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz
git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip

TESTS = test_basic test_detail test_arm test_aarch64 test_m68k test_mips test_ppc test_sparc test_tricore
TESTS = test_basic test_detail test_arm test_aarch64 test_m68k test_mips test_ppc test_sparc test_tricore test_hppa
TESTS += test_systemz test_x86 test_xcore test_iter test_evm test_riscv test_mos65xx test_wasm test_bpf test_alpha
TESTS += test_basic.static test_detail.static test_arm.static test_aarch64.static
TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static
TESTS += test_systemz.static test_x86.static test_xcore.static test_m680x.static
TESTS += test_skipdata test_skipdata.static test_iter.static test_evm.static test_riscv.static
TESTS += test_mos65xx.static test_wasm.static test_bpf.static test_alpha.static
TESTS += test_mos65xx.static test_wasm.static test_bpf.static test_alpha.static test_hppa.static

check: $(TESTS)

Expand Down
1 change: 1 addition & 0 deletions Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ DEFINE_get_detail_op(ppc, PPC);
DEFINE_get_detail_op(tricore, TriCore);
DEFINE_get_detail_op(aarch64, AArch64);
DEFINE_get_detail_op(alpha, Alpha);
DEFINE_get_detail_op(hppa, HPPA);

/// Returns true if for this architecture the
/// alias operands should be filled.
Expand Down
4 changes: 4 additions & 0 deletions Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ DECL_get_detail_op(ppc, PPC);
DECL_get_detail_op(tricore, TriCore);
DECL_get_detail_op(aarch64, AArch64);
DECL_get_detail_op(alpha, Alpha);
DECL_get_detail_op(hppa, HPPA);

/// Increments the detail->arch.op_count by one.
#define DEFINE_inc_detail_op_count(arch, ARCH) \
Expand All @@ -149,6 +150,8 @@ DEFINE_inc_detail_op_count(aarch64, AArch64);
DEFINE_dec_detail_op_count(aarch64, AArch64);
DEFINE_inc_detail_op_count(alpha, Alpha);
DEFINE_dec_detail_op_count(alpha, Alpha);
DEFINE_inc_detail_op_count(hppa, HPPA);
DEFINE_dec_detail_op_count(hppa, HPPA);

/// Returns true if a memory operand is currently edited.
static inline bool doing_mem(const MCInst *MI)
Expand All @@ -175,6 +178,7 @@ DEFINE_get_arch_detail(ppc, PPC);
DEFINE_get_arch_detail(tricore, TriCore);
DEFINE_get_arch_detail(aarch64, AArch64);
DEFINE_get_arch_detail(alpha, Alpha);
DEFINE_get_arch_detail(hppa, HPPA);

static inline bool detail_is_set(const MCInst *MI)
{
Expand Down
18 changes: 18 additions & 0 deletions MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ static inline int64_t SignExtend64(uint64_t X, unsigned B) {
return (int64_t)(X << (64 - B)) >> (64 - B);
}

/// \brief Removes the rightmost bit of x and extends the field to the left with that
/// bit to form a 64-bit quantity. The field is of size len
static inline int64_t LowSignExtend64(uint64_t x, unsigned len) {
return (x >> 1) - ((x & 1) << (len - 1));
}

/// \brief One extend number X starting at bit B and returns it as int32_t.
/// Requires 0 < B <= 32.
static inline int32_t OneExtend32(uint32_t X, unsigned B) {
Expand Down Expand Up @@ -467,4 +473,16 @@ static inline unsigned int countLeadingZeros(int x)
return count;
}

/// \brief Get specified field from 32-bit instruction. Returns bits from the segment [from, to]
static inline uint32_t get_insn_field(uint32_t insn, uint8_t from, uint8_t to)
{
return insn >> (31 - to) & ((1 << (to - from + 1)) - 1);
}

/// \brief Get specified bit from 32-bit instruction
static inline uint32_t get_insn_bit(uint32_t insn, uint8_t bit)
{
return get_insn_field(insn, bit, bit);
}

#endif
74 changes: 74 additions & 0 deletions arch/HPPA/HPPAConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Capstone Disassembly Engine */
/* By Dmitry Sibirtsev <[email protected]>, 2023 */

/* This file defines constants and macros used for parsing a HPPA instruction */

#ifndef CS_HPPA_CONSTANTS_H
#define CS_HPPA_CONSTANTS_H

#define HPPA_OP_TYPE(byte) (byte) >> 2
#define MODE_IS_HPPA_20(mode) (((mode)&CS_MODE_HPPA_20) != 0)
#define MODE_IS_HPPA_20W(mode) (((mode) & (1 << 3)) != 0)

///> HPPA opcode types
#define HPPA_OP_TYPE_SYSOP 0x00
#define HPPA_OP_TYPE_MEMMGMT 0x01
#define HPPA_OP_TYPE_ALU 0x02
#define HPPA_OP_TYPE_IDXMEM 0x03
#define HPPA_OP_TYPE_SPOP 0x04
#define HPPA_OP_TYPE_DIAG 0x05
#define HPPA_OP_TYPE_FMPYADD 0x06
#define HPPA_OP_TYPE_LDIL 0x08
#define HPPA_OP_TYPE_COPRW 0x09
#define HPPA_OP_TYPE_ADDIL 0x0a
#define HPPA_OP_TYPE_COPRDW 0x0b
#define HPPA_OP_TYPE_COPR 0x0c
#define HPPA_OP_TYPE_LDO 0x0d
#define HPPA_OP_TYPE_FLOAT 0x0e
#define HPPA_OP_TYPE_PRDSPEC 0x0f
#define HPPA_OP_TYPE_LDB 0x10
#define HPPA_OP_TYPE_LDH 0x11
#define HPPA_OP_TYPE_LDW 0x12
#define HPPA_OP_TYPE_LDWM 0x13
#define HPPA_OP_TYPE_LOADDW 0x14
#define HPPA_OP_TYPE_FLDW 0x16
#define HPPA_OP_TYPE_LOADW 0x17
#define HPPA_OP_TYPE_STB 0x18
#define HPPA_OP_TYPE_STH 0x19
#define HPPA_OP_TYPE_STW 0x1a
#define HPPA_OP_TYPE_STWM 0x1b
#define HPPA_OP_TYPE_STOREDW 0x1c
#define HPPA_OP_TYPE_FSTW 0x1e
#define HPPA_OP_TYPE_STOREW 0x1f
#define HPPA_OP_TYPE_CMPBT 0x20
#define HPPA_OP_TYPE_CMPIBT 0x21
#define HPPA_OP_TYPE_CMPBF 0x22
#define HPPA_OP_TYPE_CMPIBF 0x23
#define HPPA_OP_TYPE_CMPICLR 0x24
#define HPPA_OP_TYPE_SUBI 0x25
#define HPPA_OP_TYPE_FMPYSUB 0x26
#define HPPA_OP_TYPE_CMPBDWT 0x27
#define HPPA_OP_TYPE_ADDBT 0x28
#define HPPA_OP_TYPE_ADDIBT 0x29
#define HPPA_OP_TYPE_ADDBF 0x2a
#define HPPA_OP_TYPE_ADDIBF 0x2b
#define HPPA_OP_TYPE_ADDIT 0x2c
#define HPPA_OP_TYPE_ADDI 0x2d
#define HPPA_OP_TYPE_FPFUSED 0x2e
#define HPPA_OP_TYPE_CMPBDWF 0x2f
#define HPPA_OP_TYPE_BBS 0x30
#define HPPA_OP_TYPE_BB 0x31
#define HPPA_OP_TYPE_MOVB 0x32
#define HPPA_OP_TYPE_MOVIB 0x33
#define HPPA_OP_TYPE_SHEXDEP0 0x34
#define HPPA_OP_TYPE_SHEXDEP1 0x35
#define HPPA_OP_TYPE_SHEXDEP2 0x36
#define HPPA_OP_TYPE_BE 0x38
#define HPPA_OP_TYPE_BLE 0x39
#define HPPA_OP_TYPE_BRANCH 0x3a
#define HPPA_OP_TYPE_CMPIBDW 0x3b
#define HPPA_OP_TYPE_SHEXDEP3 0x3c
#define HPPA_OP_TYPE_SHEXDEP4 0x3d
#define HPPA_OP_TYPE_MULTMED 0x3e

#endif // CS_HPPA_CONSTANTS_H
Loading

0 comments on commit 9daa1ff

Please sign in to comment.