Monday, 14 March 2022

How to pass strings in c

Mutable strings: 

You are passing it a pointer to a string which you're not allowed to modify (you can't modify literal strings). That could be the cause of the segfault. So instead of using a pointer to the non-modifiable string literal, you could copy it to your own, modifiable buffer, like this:

char mybaz[] = "hello:world";

Immutable strings: 

if you passing it a pointer to a string which you're not allowed to modify (you can't modify literal strings). That could be the cause of the segfault.

char *mybaz = "hello:world";







Reference:

https://stackoverflow.com/questions/1863094/pass-strings-by-reference-in-c

Monday, 7 March 2022

Android vendor hook for common kernel

The vendor hook are requires to mainline vendor changes into upstream android common kernel.

Add support for vendor hooks. Adds include/trace/hooks directory for trace definition headers where hooks can be defined and vendor_hook.c for instantiating and exporting them for vendor modules.


There are two variants of vendor hooks, both based on tracepoints:

Normal: this uses the DECLARE_HOOK macro

to create a tracepoint function with the name trace_<name>where <name> is the unique identifier for the trace.

Restricted: restricted hooks are needed for cases like scheduler hooks where the attached function must be called even if the cpu is offline or requires a non-atomic context. Restricted vendor hooks cannot be detached, so modules that attach to a restricted hook can never unload. Also, only 1 attachment is allowed (any other attempts to attach will fail with-EBUSY).

For either case, modules attach to the hook by using

register_trace_<name>(func_ptr, NULL).


New hooks should be defined in headers in the

include/trace/hooks/ directory using the

DECLARE_HOOK() or DECLARE_RESTRICTED_HOOK()

macros.


New files added to include/trace/hooks should

be #include'd from drivers/android/vendor_hooks.c.

The EXPORT_TRACEPOINT_SYMBOL_GPL() should be

also added to drivers/android/vendor_hooks.c.


For example, if a new hook, 'android_vh_foo(int &ret)'

is added in do_exit() in exit.c, these changes are

needed:


1. create a new header file include/trace/hooks/foo.h

which contains:

#include <trace/hooks/vendor_hooks.h>

...

  DECLARE_HOOK(android_vh_foo,

     TP_PROTO(int *retp),

     TP_ARGS(retp);


2. in exit.c, add

#include <trace/hooks/foo.h>

...

  int ret = 0;

...

  android_vh_foo(&ret);

  if (ret)

    return ret;

...


3. in drivers/android/vendor_hooks.c, add

#include <trace/hooks/foo.h>

...

EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_foo);


The hook can then be attached by adding the registration code

to the module:


#include <trace/hooks/sched.h>

...

static void my_foo(int *retp)

{

*retp = 0;

}

...

rc = register_trace_android_vh_sched_exit(my_foo, NULL);


Reference:

https://android.googlesource.com/kernel/common/+/7f62740112ef7260d399a340e210f3a49bc4177e

Friday, 4 March 2022

Linux system call flow in ARM64

 ARMv8 has four exception has four levels.

EL0 --  user applications

EL1 --  OS kernel 

EL2  - - Hypervisor for virtualization platform

EL3  -- Secure Monitor firmware

The EL3 to EL0 elevation from one exception level to next exception level are achieved by setting exceptions. These exceptions will be set by one level and the next level will handle it.

The synchronous exception from user space EL0 to kernel EL1 using the svc supervisor call. Thus an application runs in Linux should issue svc with registers set with appropriate values. To know what are those appropriate values, Lets see how kernel handles svc.



Kernel :

Note : https://elixir.bootlin.com/linux/v5.16.10/source/arch/arm64/kernel/entry.S

Vector table :

There are multiple exceptions can be set by applications [EL0] which will be taken by Kernel [EL1]. The handlers for these exceptions are stored in a vector table. In ARMv8 the register that mentions the base address of that vector table is VBAR_EL1 [Vector Base Address Register for EL1].

When an exception occurs, the processor must execute handler code which corresponds to the exception. The location in memory where the handler is stored is called the exception vector. In the ARM architecture, exception vectors are stored in a table, called the exception vector table. Each Exception level has its own vector table, that is, there is one for each of EL3, EL2 and EL1. The table contains instructions to be executed, rather than a set of addresses. Vectors for individual exceptions are located at fixed offsets from the beginning of the table. 
 The virtual address of each table base is set by the Vector Based Address Registers VBAR_EL3, VBAR_EL2 and VBAR_EL1.

Linux defines the vector table at arch/arm64/kernel/entry.S + 493. Eachkerenl_ventry is 32 instructions long. As an instruction in ARMv8 is 4 bytes long, next kerenl_ventry will start at +0x80 of current kerenl_ventry.

ARM infocenter.

The exception-handlers reside in a continuous memory and each vector spans up to 32 instructions long. Based on type of the exception, the execution will start from an instruction in a particular offset from the base address VBAR_EL1. Below is the ARM64 vector table. For example when an synchronous exception is set from EL0 is set, the handler at VBAR_EL1 +0x400 will execute to handle the exception


Offset from VBAR_EL1Exception typeException set level
+0x000SynchronousCurrent EL with SP0
+0x080IRQ/vIRQ“
+0x100FIQ/vFIQ“
+0x180SError/vSError“
+0x200SynchronousCurrent EL with SPx
+0x280IRQ/vIRQ“
+0x300FIQ/vFIQ“
+0x380SError/vSError“
+0x400SynchronousLower EL using ARM64
+0x480IRQ/vIRQ“
+0x500FIQ/vFIQ“
+0x580SError/vSError“
+0x600SynchronousLower EL with ARM32
+0x680IRQ/vIRQ“
+0x700FIQ/vFIQ“
+0x780SError/vSError“


Linux defines the vector table at arch/arm64/kernel/entry.S + 493. Eachkerenl_ventry is 32 instructions long. As an instruction in ARMv8 is 4 bytes long, next kerenl_ventry will start at +0x80 of current kerenl_ventry.

ENTRY(vectors)
kernel_ventry 1, t, 64, sync // Synchronous EL1t
kernel_ventry 1,t, 64, irq // IRQ EL1t
kernel_ventry 1,t, 64, fiq // FIQ EL1t
kernel_ventry 1,t, 64, error // Error EL1t

kernel_ventry 1,h, 64 sync // Synchronous EL1h
kernel_ventry 1,h, 64 irq // IRQ EL1h
kernel_ventry 1,h, 64 fiq // FIQ EL1h
kernel_ventry 1,h, 64 error // Error EL1h

kernel_ventry 0,t, 64 sync // Synchronous 64-bit EL0
kernel_ventry 0,t, 64 irq // IRQ 64-bit EL0
kernel_ventry 0,t, 64 fiq // FIQ 64-bit EL0
kernel_ventry 0,t, 64 error // Error 64-bit EL0

    kernel_ventry 0,t, 32 sync // Synchronous 32-bit EL0
kernel_ventry 0,t, 32 irq // IRQ 32-bit EL0
kernel_ventry 0,t, 32 fiq // FIQ 32-bit EL0
kernel_ventry 0,t, 32 error // Error 32-bit EL0
END(vectors)

Loads the vector table into VBAR_EL1 at arch/arm64/kernel/head.S +429


adr_l   x8, vectors    // load VBAR_EL1 with virtual
msr     vbar_el1, x8   // vector table address
isb                       // instruction set barrier

VBAR_EL1 is an system register. So it cannot be accessed directly. Special system instructions msr and mrs should be used manipulate system registers.
InstructionDescription
adr_l x8, vectorloads the address of vector table into general purpose register X8
msr vbar_el1, x8moves value in X8 to system register VBAR_EL1
isbinstruction sync barrier

System call flow in Kernel

Lets see what happens when an application issues the instruction svc. From thtable, we can see for AArch64 synchronous exception from lower level, the offset is +0x400. In the Linux vector definition VBAR_EL1+0x400 is el0t_64_sync. it call el0t_64_sync_handler definition at arch/arm64/kernel/entry-common.c + 615

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
34
35
35
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
{
	unsigned long esr = read_sysreg(esr_el1); //read the syndrome register
switch (ESR_ELx_EC(esr)) { case ESR_ELx_EC_SVC64: el0_svc(regs); // SVC in 64-bit state
break; case ESR_ELx_EC_DABT_LOW: el0_da(regs, esr); // Data abort in EL0
break; case ESR_ELx_EC_IABT_LOW: el0_ia(regs, esr); // instruction abort in EL0 break; case ESR_ELx_EC_FP_ASIMD: el0_fpsimd_acc(regs, esr); // FP/ASIMD access break; case ESR_ELx_EC_SVE: el0_sve_acc(regs, esr); // SVE access in EL0 break; case ESR_ELx_EC_FP_EXC64: //FP access execution el0_fpsimd_exc(regs, esr); // break; case ESR_ELx_EC_SYS64: case ESR_ELx_EC_WFx: el0_sys(regs, esr); //configurable trap break; case ESR_ELx_EC_SP_ALIGN: el0_sp(regs, esr); //stack alignment exception break; case ESR_ELx_EC_PC_ALIGN: el0_pc(regs, esr); //PC alignment exception break; case ESR_ELx_EC_UNKNOWN: el0_undef(regs); //Unknown error break; case ESR_ELx_EC_BTI: //unallocated exception el0_bti(regs); break; case ESR_ELx_EC_BREAKPT_LOW: case ESR_ELx_EC_SOFTSTP_LOW: case ESR_ELx_EC_WATCHPT_LOW: case ESR_ELx_EC_BRK64: el0_dbg(regs, esr); //Debug exception break; case ESR_ELx_EC_FPAC: el0_fpac(regs, esr); break; default: el0_inv(regs, esr); } }

The synchronous exception can have multiple reasons which will be stored in the syndrome register esr_el1. Compare the value in syndrome register with predefined macros and branch to the corresponding subroutine.

In a system call case, control will be branched to el0_svc and it call do_e10_svc. It is defined at arm64/kernel/entry-common.c +599 and arch/arm64/kernel/syscall.c +178 as follows


/*
* SVC handler.
*/


static void noinstr el0_svc(struct pt_regs *regs) { enter_from_user_mode(regs); cortex_a76_erratum_1463225_svc_handler(); do_el0_svc(regs); exit_to_user_mode(regs); }

void do_el0_svc(struct pt_regs *regs)
{
	sve_user_discard();
	el0_svc_common(regs, regs->regs[8], __NR_syscalls, sys_call_table);
}

static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
			   const syscall_fn_t syscall_table[])
{
   invoke_syscall(regs, scno, sc_nr, syscall_table); //system call invoke here
}                                                                                                              

sys_call_table

It is nothing but an array of function pointer indexed with the system call number. It has to be placed in an 4K aligned memory. For ARM64 sys_call_table is defined at arch/arm64/kernel/sys.c +58.

#undef __SYSCALL
#define __SYSCALL(nr, sym) [nr] = sym,

/*
* The sys_call_table array must be 4K aligned to be accessible from
* kernel/entry.S.
*/
void * const sys_call_table[__NR_syscalls] __aligned(4096) = {
[0 ... __NR_syscalls - 1] = sys_ni_syscall,
#include <asm/unistd.h>
};
  • __NR_syscalls defines the number of system call. This varies from architecture to architecture.
  • Initially all the system call numbers were set sys_ni_syscall - not implemented system call. If a system call is removed, its system call number will not be reused. Instead it will be assigned with sys_ni_syscall function.
  • And the include goes like this arch/arm64/include/asm/unistd.h -> arch/arm64/include/uapi/asm/unistd.h -> include/asm-generic/unistd.h -> include/uapi/asm-generic/unistd.h. The last file has the definition of all system calls. For example the write system call is defined here as

1
2
#define __NR_write 64
__SYSCALL(__NR_write, sys_write)

  • The sys_call_table is an array of function pointers. As in ARM64 a function pointer is 8 bytes long, to calculate the address of actual system call, system call number scno is left shifted by 3 and added with system call table address.

System call definition

Each system call is defined with a macro SYSCALL_DEFINEn macro. n is corresponding to the number of arguments the system call accepts. For example the write is implemented at fs/read_write.c +652

1
2
3
4

SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
size_t, count)
{
return ksys_write(fd, buf, count);
}

This macro will expand into sys_write function definition and other aliases functions as mentioned in this LWN article. The expanded function will have the compiler directive asmlinkage set. It instructs the compiler to look for arguments in CPU stack instead of registers. This is to implement system calls architecture independent. That’s why kernel_entry macro in el0_sync pushed all general purpose registers into stack. In ARM64 case registers X0 to X7 will have the arguments.

Application Flow






Reference:

Wednesday, 2 March 2022

ARMv8-A Memory systems

 Memory management

The ARMv8-A architecture employs a weakly ordered model of memory. This means that the order of memory accesses is not necessarily required to be the same as the program order for load and store operations.
 
During the optimization process, the processor and system elements can reorder memory read operations with respect to each other to improve data throughput. Writes can also be reordered. This means that the required bandwidth between the processor and external memory can be reduced and the long latencies that are associated with such external memory accesses are hidden.

 To ensure that reordering can take place, there must be memory types that allow such optimizations to take place in them. 

Hardware can reorder reads and writes to Normal memory. Reads and writes can also be ordered by address dependencies, and half barriers. However, the existence of either data dependencies or explicit memory barrier instructions can override this. Certain situations require stronger ordering rules. You can provide information to the core about this through the memory type attribute of the translation table entry that describes that memory. 

High-performance systems can support techniques such as speculative memory reads, multiple issuing of instructions, or out-of-order execution and these, along with other techniques, offer further possibilities for hardware reordering of memory access:

Page table entry

  • From virtual to physical address translation, as well as attributes for that address
  • Some bits are for OS, such as dirty and accessed (PTE_YOUNG / PTE_OLD in Linux)

Memory types = normal

    Normal memory is used for all code and for most data regions in memory. Examples of Normal memory include areas of RAM, Flash, or ROM in physical memory. This kind of memory provides the highest processor performance as it is weakly ordered and the compiler can perform more optimizations. The processor can reorder, repeat, and merge accesses to Normal memory.

  • Reordering
  • Merging
  • Speculation 
  • Unaligned
  • either cacheable or non-cacheable are OK

Memory type = device

The Device memory type is used with memory-mapped peripherals and all memory regions where an access might have a side effect. For example, a read to a timer is not repeatable, as it returns different values for each read. A write to a control register can trigger an interrupt. The Device memory type imposes more restrictions on the core.

Speculative data accesses cannot be performed to regions of memory that are marked as Device. Trying to execute code from a region marked as Device is UNPREDICTABLE. 
 
  • Side effects
  • Cannot do speculative access
  • Cannot be executable
  • Attributes
    • Gathering?
    • Re-ordering?
    • Early ack?
  • Device type: stronger to weaker
    • GRE -> nGRE -> nGnRE -> nGnRnE
    • Can upgrade to a weaker type

Barrier :

The ARM architecture includes barrier instructions to force access ordering and access completion at a specific point. Barriers are used to prevent unsafe optimizations from occurring and to enforce a specific memory ordering. Use of unnecessary barrier instructions can therefore reduce software performance. Consider carefully whether a barrier is necessary in a specific situation, and if so, which is the correct barrier to use.

       ISB     -- Instruction synchronization barrier   
       DMB  -- Data memory barrier
       DSB   -- Data synchronization barrier



MMU (memory management unit)

  • Software defines the translation, MMU in charge of reading that table and provide the translation service to the core
  • TLB (translation look-aside buffer) + PTW (page table walker)
    • TLB of most modern ARM cores also caches intermediate steps of translation to speed up the process
  • MMU is before the cache, so cache works with physical address and won’t be affected by changes in address translation

Virtual address space

AArch64 uses 48-bit virtual address, and there are 2 of them. One for kernel (not avaible in EL2 and EL3), one for application. So there are 2 sets of translation tables, which are both in memory. TTBR is pointing to the translation table base.

Translation table (page table)

  • 3-level of tables
  • 3 different sizes of page
    • 4KB, 16KB or 64KB

Translation regimes

  • EL3 secure monitor table
  • EL2 hypervisor table
  • EL1/EL0 goes through 2 stages of translation tables for virtualization

Translation regimes

Secure physical address spaces

  • Secure vs non-secure
    • Non-secure program in EL1/EL0 can only access non-secure physical address
    • Secure EL1/EL0 programs can access both

References:

https://developer.arm.com/documentation/100941/0100/Memory-typesarmv8_a_memory_systems_100941_0100_en.pdf

https://phdbreak99.github.io/blog/arch/2019-03-18-armv8-architecture/

https://stackoverflow.com/questions/65684882/is-memory-reordering-equivalent-to-instruction-reordering