Skip to content

Commit f306a2a

Browse files
committed
fix: 6.12 kernel support and disable SIMD
- Disable SIMD by adding -mgeneral-regs-only compiler flag - Fix paging_init hook: use bl instruction hook instead of direct function hook - Add vmalloc_noprof symbol support for 6.12 kernel - Add memset wrapper function - Fix copy_to_user function fallback order - Improve map_data stack allocation - Update kptools to locate paging_init bl instruction in setup_arch
1 parent 0d80e99 commit f306a2a

File tree

11 files changed

+52
-56
lines changed

11 files changed

+52
-56
lines changed

kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LD = $(TARGET_COMPILE)ld
99
AS = $(TARGET_COMPILE)as
1010
OBJCOPY = $(TARGET_COMPILE)objcopy
1111

12-
CFLAGS += -Wall -fno-builtin -std=gnu11 -nostdinc
12+
CFLAGS += -Wall -fno-builtin -std=gnu11 -nostdinc -mgeneral-regs-only
1313
CFLAGS += -g
1414

1515
ifdef DEBUG

kernel/base/baselib.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,8 @@ char *lib_strstr(const char *haystack, const char *needle)
345345
{
346346
return (char *)lib_memmem(haystack, lib_strlen(haystack), needle, lib_strlen(needle));
347347
}
348+
349+
void *memset(void *dst, int c, size_t n)
350+
{
351+
return lib_memset(dst, c, n);
352+
}

kernel/base/map.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ static void flush_icache_all(void)
6666
asm volatile("isb" : : : "memory");
6767
}
6868

69-
static map_data_t *mem_proc()
69+
static void mem_proc(map_data_t *data)
7070
{
71-
map_data_t *data = get_data();
71+
*data = *get_data();
7272
uint64_t kernel_va = get_kva();
7373

7474
// relocation
@@ -105,8 +105,6 @@ static map_data_t *mem_proc()
105105
uint64_t detect_virt = (uint64_t)((memblock_virt_alloc_try_nid_f)data->map_symbol.memblock_virt_alloc_relo)(
106106
0, 0x10, detect_phys, detect_phys, NUMA_NO_NODE);
107107
data->linear_voffset = detect_virt - detect_phys;
108-
109-
return data;
110108
}
111109

112110
// todo: 52-bits pa
@@ -175,7 +173,10 @@ static uint64_t __noinline get_or_create_pte(map_data_t *data, uint64_t va, uint
175173
// todo: bti
176174
void __noinline _paging_init()
177175
{
178-
map_data_t *data = mem_proc();
176+
map_data_t buf;
177+
map_data_t *data = &buf;
178+
mem_proc(data);
179+
179180
#ifdef MAP_DEBUG
180181
printk_f printk = (printk_f)(data->printk_relo);
181182
#define map_debug(idx, val) printk(data->str_fmt_px, idx, val)
@@ -202,10 +203,7 @@ void __noinline _paging_init()
202203
((memblock_mark_nomap_f)(data->map_symbol.memblock_mark_nomap_relo))(start_pa, all_size);
203204

204205
// paging_init
205-
uint64_t paging_init_va = data->paging_init_relo;
206-
*(uint32_t *)(paging_init_va) = data->paging_init_backup;
207-
flush_icache_all();
208-
((paging_init_f)(paging_init_va))();
206+
((paging_init_f)(data->paging_init_relo))();
209207
// can't write data below
210208

211209
// AttrIndx[2:0] encoding
@@ -243,7 +241,7 @@ void __noinline _paging_init()
243241
}
244242

245243
flush_icache_all();
246-
244+
247245
// free old start
248246
((memblock_free_f)data->map_symbol.memblock_free_relo)(old_start_pa, reserve_size);
249247

kernel/base/setup.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
typedef struct
2626
{
2727
// preset
28-
uint32_t paging_init_backup;
29-
uint32_t __;
3028
int64_t map_offset;
3129
int64_t start_offset;
3230
int64_t start_size;
@@ -49,8 +47,7 @@ typedef struct
4947
uint64_t linear_voffset;
5048
} map_data_t;
5149
#else
52-
#define map_paging_init_backup_offset 0
53-
#define map_map_offset_offset (map_paging_init_backup_offset + 8)
50+
#define map_map_offset_offset 0
5451
#define map_start_offset_offset (map_map_offset_offset + 8)
5552
#define map_start_size_offset (map_start_offset_offset + 8)
5653
#define map_start_img_size_offset (map_start_size_offset + 8)

kernel/base/setup1.S

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ map_prepare:
182182
// map_data.paging_init_relo = setup_preset.paging_init_offset;
183183
ldr x11, [x10, #setup_paging_init_offset_offset]
184184
str x11, [x9, #map_paging_init_relo_offset]
185-
mov x15, x11
185+
186+
ldr x15, [x10, #setup_paging_init_bl_offset_offset]
187+
// uint64_t paging_init_bl_pa = paging_init_bl_offset + kernel_pa;
188+
add x13, x15, x19
186189

187190
// map_data.map_symbol = setup_preset.map_symbol
188191
add x0, x9, #map_map_symbol_offset
@@ -218,37 +221,6 @@ map_prepare:
218221
add x11, x11, #MEMORY_RW_SIZE
219222
str x11, [x9, #map_alloc_size_offset]
220223

221-
// backup and hook paging_init
222-
// uint64_t paging_init_pa = paging_init_offset + kernel_pa;
223-
add x13, x15, x19
224-
// map_data.paging_init_backup = *(uint32_t *)(paging_init_pa);
225-
ldr w12, [x13]
226-
227-
mov w3, #0x201F
228-
movk w3, #0xD503, lsl#16
229-
orr w1, w3, #0x100
230-
mov w2, #0xFFFFFD1F
231-
and w0, w12, w2
232-
// if ((map_data.paging_init_backup & 0xFFFFFD1F) == 0xD503211F)
233-
cmp w0, w1
234-
b.ne .backup
235-
// map_data.paging_init_backup = NOP
236-
mov w12, w3
237-
// uint32_t *p = (uint32_t *)paging_init_pa + 1;
238-
add x11, x13, #4
239-
.cmp_auti:
240-
// while ((*p & 0xFFFFFD1F) != 0xD503211F) ++p;
241-
ldr w0, [x11], #4
242-
and w0, w0, w2
243-
cmp w0, w1
244-
b.ne .cmp_auti
245-
// *p = NOP
246-
stur w3, [x11, #-4]
247-
248-
.backup:
249-
str w12, [x9, #map_paging_init_backup_offset]
250-
dsb ish
251-
252224
// uint64_t replace_offset = (uint64_t)(_paging_init - _map_start) + map_offset;
253225
adrp x11, _paging_init
254226
add x11, x11, :lo12:_paging_init
@@ -257,11 +229,11 @@ map_prepare:
257229
sub x11, x11, x12
258230
add x11, x11, x14
259231

260-
// *(uint32_t *)paging_init_pa = B_REL(paging_init_offset, replace_offset);
261-
// #define B_REL(src, dst) (0x14000000u | (((dst - src) & 0x0FFFFFFFu) >> 2u))
232+
// *(uint32_t *)paging_init_bl_pa = B_REL(paging_init_bl_offset, replace_offset);
233+
// #define BL_REL(src, dst) (0x94000000u | (((dst - src) & 0x0FFFFFFFu) >> 2u))
262234
sub x15, x11, x15
263235
ubfx w15, w15, #2, #26
264-
mov w12, #0x14000000
236+
mov w12, #0x94000000
265237
orr w15, w15, w12
266238
str w15, [x13]
267239

kernel/include/preset.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ typedef struct _setup_preset_t
236236
int64_t map_max_size;
237237
int64_t kallsyms_lookup_name_offset;
238238
int64_t paging_init_offset;
239+
int64_t paging_init_bl_offset;
239240
int64_t printk_offset;
240241
map_symbol_t map_symbol;
241242
uint8_t header_backup[HDR_BACKUP_SIZE];
@@ -258,7 +259,8 @@ typedef struct _setup_preset_t
258259
#define setup_map_max_size_offset (setup_map_offset_offset + 8)
259260
#define setup_kallsyms_lookup_name_offset_offset (setup_map_max_size_offset + 8)
260261
#define setup_paging_init_offset_offset (setup_kallsyms_lookup_name_offset_offset + 8)
261-
#define setup_printk_offset_offset (setup_paging_init_offset_offset + 8)
262+
#define setup_paging_init_bl_offset_offset (setup_paging_init_offset_offset + 8)
263+
#define setup_printk_offset_offset (setup_paging_init_bl_offset_offset + 8)
262264
#define setup_map_symbol_offset (setup_printk_offset_offset + 8)
263265
#define setup_header_backup_offset (setup_map_symbol_offset + MAP_SYMBOL_SIZE)
264266
#define setup_superkey_offset (setup_header_backup_offset + HDR_BACKUP_SIZE)

kernel/linux/include/linux/vmalloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extern void *kfunc_def(vm_map_ram)(struct page **pages, unsigned int count, int
6464
extern void kfunc_def(vm_unmap_aliases)(void);
6565

6666
extern void *kfunc_def(vmalloc)(unsigned long size);
67+
extern void *kfunc_def(vmalloc_noprof)(unsigned long size);
6768
extern void *kfunc_def(vzalloc)(unsigned long size);
6869
extern void *kfunc_def(vmalloc_user)(unsigned long size);
6970
extern void *kfunc_def(vmalloc_node)(unsigned long size, int node);
@@ -121,6 +122,7 @@ static inline void vm_unmap_aliases(void)
121122
static inline void *vmalloc(unsigned long size)
122123
{
123124
kfunc_call(vmalloc, size);
125+
kfunc_call(vmalloc_noprof, size);
124126
kfunc_not_found();
125127
return 0;
126128
}

kernel/patch/common/utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ int __must_check compat_copy_to_user(void __user *to, const void *from, int n)
7575
{
7676
int cplen = 0;
7777

78-
if (kfunc(seq_buf_to_user)) {
79-
cplen = seq_buf_copy_to_user(to, from, n);
80-
} else if (kfunc(xt_data_to_user)) {
78+
if (kfunc(xt_data_to_user)) {
8179
// xt_data_to_user, xt_obj_to_user
8280
cplen = compat_xt_data_copy_to_user(to, from, n);
8381
if (!cplen) cplen = n;
8482
} else if (kfunc(bits_to_user)) {
8583
// bits_to_user, str_to_user
8684
cplen = compat_bits_copy_to_user(to, from, n);
85+
} else if (kfunc(seq_buf_to_user)) {
86+
cplen = seq_buf_copy_to_user(to, from, n);
8787
} else if (kfunc(trace_seq_to_user)) {
8888
cplen = trace_seq_copy_to_user(to, from, n);
8989
} else {

kernel/patch/ksyms/misc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ void *kfunc_def(vm_map_ram)(struct page **pages, unsigned int count, int node) =
336336
void kfunc_def(vm_unmap_aliases)(void) = 0;
337337

338338
void *kfunc_def(vmalloc)(unsigned long size) = 0;
339+
void *kfunc_def(vmalloc_noprof)(unsigned long size) = 0;
339340
void *kfunc_def(vzalloc)(unsigned long size) = 0;
340341
void *kfunc_def(vmalloc_user)(unsigned long size) = 0;
341342
void *kfunc_def(vmalloc_node)(unsigned long size, int node) = 0;
@@ -383,6 +384,7 @@ static void _linux_mm_vmalloc_sym_match(const char *name, unsigned long addr)
383384
// kfunc_match(vm_unmap_aliases, name, addr);
384385

385386
kfunc_match(vmalloc, name, addr);
387+
kfunc_match(vmalloc_noprof, name, addr);
386388
kfunc_match(vzalloc, name, addr);
387389
// kfunc_match(vmalloc_user, name, addr);
388390
// kfunc_match(vmalloc_node, name, addr);

tools/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern bool log_enable;
4444
#define align_ceil(x, align) (((uint64_t)(x) + (uint64_t)(align)-1) & ~((uint64_t)(align)-1))
4545

4646
#define INSN_IS_B(inst) (((inst) & 0xFC000000) == 0x14000000)
47+
#define INSN_IS_BL(inst) (((inst) & 0xFC000000) == 0x94000000)
4748

4849
#define bits32(n, high, low) ((uint32_t)((n) << (31u - (high))) >> (31u - (high) + (low)))
4950

0 commit comments

Comments
 (0)