mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-21 20:23:18 +00:00
GITBOOK-4236: change request with no subject merged in GitBook
This commit is contained in:
parent
25a3848ed2
commit
2fc0778f90
5 changed files with 66 additions and 3 deletions
BIN
.gitbook/assets/image (725).png
Normal file
BIN
.gitbook/assets/image (725).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
.gitbook/assets/image (726).png
Normal file
BIN
.gitbook/assets/image (726).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
.gitbook/assets/image (727).png
Normal file
BIN
.gitbook/assets/image (727).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
.gitbook/assets/image (728).png
Normal file
BIN
.gitbook/assets/image (728).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 57 KiB |
|
@ -41,7 +41,7 @@ ARM64 has **31 general-purpose registers**, labeled `x0` through `x30`. Each can
|
|||
* **`x0`** also carries the return data of a function
|
||||
2. **`x8`** - In the Linux kernel, `x8` is used as the system call number for the `svc` instruction. **In macOS the x16 is the one used!**
|
||||
3. **`x9`** to **`x15`** - More temporary registers, often used for local variables.
|
||||
4. **`x16`** and **`x17`** - **Intraprocedural Call Registers**. Temporary registers for immediate values. They are also used for indirect function calls and PLT (Procedure Linkage Table) stubs.
|
||||
4. **`x16`** and **`x17`** - **Intra-procedural Call Registers**. Temporary registers for immediate values. They are also used for indirect function calls and PLT (Procedure Linkage Table) stubs.
|
||||
* **`x16`** is used as the **system call number** for the **`svc`** instruction in **macOS**.
|
||||
5. **`x18`** - **Platform register**. It can be used as a general-purpose register, but on some platforms, this register is reserved for platform-specific uses: Pointer to current thread environment block in Windows, or to point to the currently **executing task structure in linux kernel**.
|
||||
6. **`x19`** to **`x28`** - These are callee-saved registers. A function must preserve these registers' values for its caller, so they are stored in the stack and recovered before going back to the caller.
|
||||
|
@ -51,7 +51,7 @@ ARM64 has **31 general-purpose registers**, labeled `x0` through `x30`. Each can
|
|||
* It could also be used like any other register.
|
||||
9. **`sp`** - **Stack pointer**, used to keep track of the top of the stack.
|
||||
* the **`sp`** value should always be kept to at least a **quadword** **alignment** or a alignment exception may occur.
|
||||
10. **`pc`** - **Program counter**, which points to the current instruction. This register can only be updates through exception generations, exception returns, and branches. The only ordinary instructions that can read this register are branch with link instructions (BL, BLR) to store the **`pc`** address in **`lr`** (Link Register).
|
||||
10. **`pc`** - **Program counter**, which points to the next instruction. This register can only be updates through exception generations, exception returns, and branches. The only ordinary instructions that can read this register are branch with link instructions (BL, BLR) to store the **`pc`** address in **`lr`** (Link Register).
|
||||
11. **`xzr`** - **Zero register**. Also called **`wzr`** in it **32**-bit register form. Can be used to get the zero value easily (common operation) or to perform comparisons using **`subs`** like **`subs XZR, Xn, #10`** storing the resulting data nowhere (in **`xzr`**).
|
||||
|
||||
The **`Wn`** registers are the **32bit** version of the **`Xn`** register.
|
||||
|
@ -73,7 +73,8 @@ They are often used to store the b**ase address of the thread-local storage** re
|
|||
|
||||
### **PSTATE**
|
||||
|
||||
**PSTATE** is several components serialized into the operating-system-visible **`SPSR_ELx`** special register. These are the accessible fields:
|
||||
**PSTATE** contains several process components serialized into the operating-system-visible **`SPSR_ELx`** special register, being X the **permission** **level of the triggered** exception (this allows to recover the process state when the exception ends).\
|
||||
These are the accessible fields:
|
||||
|
||||
* The **`N`**, **`Z`**, **`C`** and **`V`** condition flags:
|
||||
* **`N`** means the operation yielded a negative result
|
||||
|
@ -88,6 +89,7 @@ They are often used to store the b**ase address of the thread-local storage** re
|
|||
* The **single stepping** flag (**`SS`**): Used by debuggers to single step by setting the SS flag to 1 inside **`SPSR_ELx`** through an exception. The program will run a step and issue a single step exception.
|
||||
* The **illegal exception** state flag (**`IL`**): It's used to mark when a privileged software performs an invalid exception level transfer, this flag is set to 1 and the processor triggers an illegal state exception.
|
||||
* The **`DAIF`** flags: These flags allow a privileged program to selectively mask certain external exceptions.
|
||||
* If **`A`** is 1 it means **asynchronous aborts** will be triggered. The **`I`** configures to respond to external hardware **Interrupts Requests** (IRQs). and the F is related to **Fast Interrupt Requests** (FIRs).
|
||||
* The **stack pointer select** flags (**`SPS`**): Privileged programs running in EL1 and above can swap between using their own stack pointer register and the user-model one (e.g. between `SP_EL1` and `EL0`). This switching is performed by writing to the **`SPSel`** special register. This cannot be done from EL0.
|
||||
|
||||
<figure><img src="../../../.gitbook/assets/image (724).png" alt=""><figcaption></figcaption></figure>
|
||||
|
@ -186,6 +188,67 @@ Armv8-A support the execution of 32-bit programs. **AArch32** can run in one of
|
|||
**Privileged** 64-bit programs can schedule the **execution of 32-bit** programs by executing a exception level transfer to the lower privileged 32-bit.\
|
||||
Note that the transition from 64-bit to 32-bit occurs with a lower of the exception level (for example a 64-bit program in EL1 triggering a program in EL0). This is done by setting the **bit 4 of** **`SPSR_ELx`** special register **to 1** when the `AArch32` process thread is ready to be executed and the rest of `SPSR_ELx` stores the **`AArch32`** programs CPSR. Then, the privileged process calls the **`ERET`** instruction so the processor transitions to **`AArch32`** entering in A32 or T32 depending on CPSR**.**
|
||||
|
||||
The **`interworking`** occurs using the J and T bits of CPSR. `J=0` and `T=0` means **`A32`** and `J=0` and `T=1` means **T32**. This basically traduces on setting the **lowest bit to 1** to indicate the instruction set is T32.\
|
||||
This is set during the **interworking branch instructions,** but can also be set directly with other instructions when the PC is set as the destination register. Example:
|
||||
|
||||
Another example:
|
||||
|
||||
```armasm
|
||||
_start:
|
||||
.code 32 ; Begin using A32
|
||||
add r4, pc, #1 ; Here PC is already pointing to "mov r0, #0"
|
||||
bx r4 ; Swap to T32 mode: Jump to "mov r0, #0" + 1 (so T32)
|
||||
|
||||
.code 16:
|
||||
mov r0, #0
|
||||
mov r0, #8
|
||||
```
|
||||
|
||||
### Registers
|
||||
|
||||
There are 16 32-bit registers (r0-r15). **From r0 to r14** they can be used for **any operation**, however some of them are usually reserved:
|
||||
|
||||
* **`r15`**: Program counter (always). Contains the address of the next instruction. In A32 current + 8, in T32, current + 4.
|
||||
* **`r11`**: Frame Pointer
|
||||
* **`r12`**: Intra-procedural call register
|
||||
* **`r13`**: Stack Pointer
|
||||
* **`r14`**: Link Register
|
||||
|
||||
Moreover, registers are backed up in **`banked registries`**. Which are places that store the registers values allowing to perform **fast context switching** in exception handling and privileged operations to avoid the need to manually save and restore registers every time.\
|
||||
This is done by **saving the processor state from the `CPSR` to the `SPSR`** of the processor mode to which the exception is taken. On the exception returns, the **`CPSR`** is restored from the **`SPSR`**.
|
||||
|
||||
### CPSR - Current Program Status Register
|
||||
|
||||
In AArch32 the CPSR works similar to **`PSTATE`** in AArch64 and is also stored in **`SPSR_ELx`** when a exception is taken to restore later the execution:
|
||||
|
||||
<figure><img src="../../../.gitbook/assets/image (725).png" alt=""><figcaption></figcaption></figure>
|
||||
|
||||
The fields are divided in some groups:
|
||||
|
||||
* Application Program Status Register (APSR): Arithmetic flags and accesible from EL0
|
||||
* Execution State Registers: Process behaviour (managed by the OS).
|
||||
|
||||
#### Application Program Status Register (APSR)
|
||||
|
||||
* The **`N`**, **`Z`**, **`C`**, **`V`** flags (just like in AArch64)
|
||||
* The **`Q`** flag: It's set to 1 whenever **integer saturation occurs** during the execution of a specialized saturating arithmetic instruction. Once it's set to **`1`**, it'll maintain the value until it's manually set to 0. Moreover, there isn't any instruction that checks its value implicitly, it must be done reading it manually.
|
||||
* **`GE`** (Greater than or equal) Flags: It's used in SIMD (Single Instruction, Multiple Data) operations, such as "parallel add" and "parallel subtract". These operations allow processing multiple data points in a single instruction.
|
||||
|
||||
For example, the **`UADD8`** instruction **adds four pairs of bytes** (from two 32-bit operands) in parallel and stores the results in a 32-bit register. It then **sets the `GE` flags in the `APSR`** based on these results. Each GE flag corresponds to one of the byte additions, indicating if the addition for that byte pair **overflowed**.
|
||||
|
||||
The **`SEL`** instruction uses these GE flags to perform conditional actions.
|
||||
|
||||
#### Execution State Registers
|
||||
|
||||
* The **`J`** and **`T`** bits: **`J`** should be 0 and if **`T`** is 0 the instruction set A32 is used, and if it's 1, the T32 is used.
|
||||
* **IT Block State Register** (`ITSTATE`): These are the bits from 10-15 and 25-26. They store conditions for instructions inside an **`IT`** prefixed group.
|
||||
* **`E`** bit: Indicates the **endianness**. 
|
||||
* **Mode and Exception Mask Bits** (0-4): They determine the current execution state. The **5th** one indicates if the program runs as 32bit (a 1) or 64bit (a 0). The other 4 represents the **exception mode currently in used** (when a exception occurs and it's being handled). The number set **indicates the current priority** in case another exception is triggered while this is being handled.
|
||||
|
||||
<figure><img src="../../../.gitbook/assets/image (728).png" alt=""><figcaption></figcaption></figure>
|
||||
|
||||
* **`AIF`**: Certain exceptions can be disabled using the bits **`A`**, `I`, `F`. If **`A`** is 1 it means **asynchronous aborts** will be triggered. The **`I`** configures to respond to external hardware **Interrupts Requests** (IRQs). and the F is related to **Fast Interrupt Requests** (FIRs).
|
||||
|
||||
## macOS
|
||||
|
||||
### BSD syscalls
|
||||
|
|
Loading…
Reference in a new issue