Skip to content

Commit 4bcb4ad

Browse files
committed
MIPS: Consistently declare TLB functions
Since at least the beginning of the git era we've declared our TLB exception handling functions inconsistently. They're actually functions, but we declare them as arrays of u32 where each u32 is an encoded instruction. This has always been the case for arch/mips/mm/tlbex.c, and has also been true for arch/mips/kernel/traps.c since commit 86a1708 ("MIPS: Make tlb exception handler definitions and declarations match.") which aimed for consistency but did so by consistently making the our C code inconsistent with our assembly. This is all usually harmless, but when using GCC 7 or newer to build a kernel targeting microMIPS (ie. CONFIG_CPU_MICROMIPS=y) it becomes problematic. With microMIPS bit 0 of the program counter indicates the ISA mode. When bit 0 is zero instructions are decoded using the standard MIPS32 or MIPS64 ISA. When bit 0 is one instructions are decoded using microMIPS. This means that function pointers become odd - their least significant bit is one for microMIPS code. We work around this in cases where we need to access code using loads & stores with our msk_isa16_mode() macro which simply clears bit 0 of the value it is given: #define msk_isa16_mode(x) ((x) & ~0x1) For example we do this for our TLB load handler in build_r4000_tlb_load_handler(): u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl); We then write code to p, expecting it to be suitably aligned (our LEAF macro aligns functions on 4 byte boundaries, so (ulong)handle_tlbl will give a value one greater than a multiple of 4 - ie. the start of a function on a 4 byte boundary, with the ISA mode bit 0 set). This worked fine up to GCC 6, but GCC 7 & onwards is smart enough to presume that handle_tlbl which we declared as an array of u32s must be aligned sufficiently that bit 0 of its address will never be set, and as a result optimize out msk_isa16_mode(). This leads to p having an address with bit 0 set, and when we go on to attempt to store code at that address we take an address error exception due to the unaligned memory access. This leads to an exception prior to the kernel having configured its own exception handlers, so we jump to whatever handlers the bootloader configured. In the case of QEMU this results in a silent hang, since it has no useful general exception vector. Fix this by consistently declaring our TLB-related functions as functions. For handle_tlbl(), handle_tlbs() & handle_tlbm() we do this in asm/tlbex.h & we make use of the existing declaration of tlbmiss_handler_setup_pgd() in asm/mmu_context.h. Our TLB handler generation code in arch/mips/mm/tlbex.c is adjusted to deal with these definitions, in most cases simply by casting the function pointers to u32 pointers. This allows us to include asm/mmu_context.h in arch/mips/mm/tlbex.c to get the definitions of tlbmiss_handler_setup_pgd & pgd_current, removing some needless duplication. Consistently using msk_isa16_mode() on function pointers means we no longer need the tlbmiss_handler_setup_pgd_start symbol so that is removed entirely. Now that we're declaring our functions as functions GCC stops optimizing out msk_isa16_mode() & a microMIPS kernel built with either GCC 7.3.0 or 8.1.0 boots successfully. Signed-off-by: Paul Burton <paul.burton@mips.com>
1 parent b29fea3 commit 4bcb4ad

5 files changed

Lines changed: 51 additions & 64 deletions

File tree

‎arch/mips/include/asm/mmu_context.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ do { \
3232
} while (0)
3333

3434
extern void tlbmiss_handler_setup_pgd(unsigned long);
35+
extern char tlbmiss_handler_setup_pgd_end[];
3536

3637
/* Note: This is also implemented with uasm in arch/mips/kvm/entry.c */
3738
#define TLBMISS_HANDLER_SETUP_PGD(pgd) \

‎arch/mips/include/asm/tlbex.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ void build_tlb_write_entry(u32 **p, struct uasm_label **l,
2424
struct uasm_reloc **r,
2525
enum tlb_write_entry wmode);
2626

27+
extern void handle_tlbl(void);
28+
extern char handle_tlbl_end[];
29+
30+
extern void handle_tlbs(void);
31+
extern char handle_tlbs_end[];
32+
33+
extern void handle_tlbm(void);
34+
extern char handle_tlbm_end[];
35+
2736
#endif /* __ASM_TLBEX_H */

‎arch/mips/kernel/traps.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@
6767
#include <asm/mmu_context.h>
6868
#include <asm/types.h>
6969
#include <asm/stacktrace.h>
70+
#include <asm/tlbex.h>
7071
#include <asm/uasm.h>
7172

7273
extern void check_wait(void);
7374
extern asmlinkage void rollback_handle_int(void);
7475
extern asmlinkage void handle_int(void);
75-
extern u32 handle_tlbl[];
76-
extern u32 handle_tlbs[];
77-
extern u32 handle_tlbm[];
7876
extern asmlinkage void handle_adel(void);
7977
extern asmlinkage void handle_ades(void);
8078
extern asmlinkage void handle_ibe(void);

‎arch/mips/mm/tlb-funcs.S‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#define FASTPATH_SIZE 128
1919

20-
EXPORT(tlbmiss_handler_setup_pgd_start)
2120
LEAF(tlbmiss_handler_setup_pgd)
2221
1: j 1b /* Dummy, will be replaced. */
2322
.space 64

‎arch/mips/mm/tlbex.c‎

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <asm/cacheflush.h>
3333
#include <asm/cpu-type.h>
34+
#include <asm/mmu_context.h>
3435
#include <asm/pgtable.h>
3536
#include <asm/war.h>
3637
#include <asm/uasm.h>
@@ -253,8 +254,10 @@ static void output_pgtable_bits_defines(void)
253254
pr_debug("\n");
254255
}
255256

256-
static inline void dump_handler(const char *symbol, const u32 *handler, int count)
257+
static inline void dump_handler(const char *symbol, const void *start, const void *end)
257258
{
259+
unsigned int count = (end - start) / sizeof(u32);
260+
const u32 *handler = start;
258261
int i;
259262

260263
pr_debug("LEAF(%s)\n", symbol);
@@ -402,12 +405,6 @@ static void build_restore_work_registers(u32 **p)
402405
* CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
403406
* we cannot do r3000 under these circumstances.
404407
*
405-
* Declare pgd_current here instead of including mmu_context.h to avoid type
406-
* conflicts for tlbmiss_handler_setup_pgd
407-
*/
408-
extern unsigned long pgd_current[];
409-
410-
/*
411408
* The R3000 TLB handler is simple.
412409
*/
413410
static void build_r3000_tlb_refill_handler(void)
@@ -444,8 +441,7 @@ static void build_r3000_tlb_refill_handler(void)
444441

445442
memcpy((void *)ebase, tlb_handler, 0x80);
446443
local_flush_icache_range(ebase, ebase + 0x80);
447-
448-
dump_handler("r3000_tlb_refill", (u32 *)ebase, 32);
444+
dump_handler("r3000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x80));
449445
}
450446
#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
451447

@@ -1465,8 +1461,7 @@ static void build_r4000_tlb_refill_handler(void)
14651461

14661462
memcpy((void *)ebase, final_handler, 0x100);
14671463
local_flush_icache_range(ebase, ebase + 0x100);
1468-
1469-
dump_handler("r4000_tlb_refill", (u32 *)ebase, 64);
1464+
dump_handler("r4000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x100));
14701465
}
14711466

14721467
static void setup_pw(void)
@@ -1568,30 +1563,21 @@ static void build_loongson3_tlb_refill_handler(void)
15681563
uasm_resolve_relocs(relocs, labels);
15691564
memcpy((void *)(ebase + 0x80), tlb_handler, 0x80);
15701565
local_flush_icache_range(ebase + 0x80, ebase + 0x100);
1571-
dump_handler("loongson3_tlb_refill", (u32 *)(ebase + 0x80), 32);
1566+
dump_handler("loongson3_tlb_refill",
1567+
(u32 *)(ebase + 0x80), (u32 *)(ebase + 0x100));
15721568
}
15731569

1574-
extern u32 handle_tlbl[], handle_tlbl_end[];
1575-
extern u32 handle_tlbs[], handle_tlbs_end[];
1576-
extern u32 handle_tlbm[], handle_tlbm_end[];
1577-
extern u32 tlbmiss_handler_setup_pgd_start[];
1578-
extern u32 tlbmiss_handler_setup_pgd[];
1579-
extern u32 tlbmiss_handler_setup_pgd_end[];
1580-
15811570
static void build_setup_pgd(void)
15821571
{
15831572
const int a0 = 4;
15841573
const int __maybe_unused a1 = 5;
15851574
const int __maybe_unused a2 = 6;
1586-
u32 *p = tlbmiss_handler_setup_pgd_start;
1587-
const int tlbmiss_handler_setup_pgd_size =
1588-
tlbmiss_handler_setup_pgd_end - tlbmiss_handler_setup_pgd_start;
1575+
u32 *p = (u32 *)msk_isa16_mode((ulong)tlbmiss_handler_setup_pgd);
15891576
#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
15901577
long pgdc = (long)pgd_current;
15911578
#endif
15921579

1593-
memset(tlbmiss_handler_setup_pgd, 0, tlbmiss_handler_setup_pgd_size *
1594-
sizeof(tlbmiss_handler_setup_pgd[0]));
1580+
memset(p, 0, tlbmiss_handler_setup_pgd_end - (char *)p);
15951581
memset(labels, 0, sizeof(labels));
15961582
memset(relocs, 0, sizeof(relocs));
15971583
pgd_reg = allocate_kscratch();
@@ -1644,15 +1630,15 @@ static void build_setup_pgd(void)
16441630
else
16451631
uasm_i_nop(&p);
16461632
#endif
1647-
if (p >= tlbmiss_handler_setup_pgd_end)
1633+
if (p >= (u32 *)tlbmiss_handler_setup_pgd_end)
16481634
panic("tlbmiss_handler_setup_pgd space exceeded");
16491635

16501636
uasm_resolve_relocs(relocs, labels);
16511637
pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
1652-
(unsigned int)(p - tlbmiss_handler_setup_pgd));
1638+
(unsigned int)(p - (u32 *)tlbmiss_handler_setup_pgd));
16531639

16541640
dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
1655-
tlbmiss_handler_setup_pgd_size);
1641+
tlbmiss_handler_setup_pgd_end);
16561642
}
16571643

16581644
static void
@@ -1921,12 +1907,11 @@ build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
19211907

19221908
static void build_r3000_tlb_load_handler(void)
19231909
{
1924-
u32 *p = handle_tlbl;
1925-
const int handle_tlbl_size = handle_tlbl_end - handle_tlbl;
1910+
u32 *p = (u32 *)handle_tlbl;
19261911
struct uasm_label *l = labels;
19271912
struct uasm_reloc *r = relocs;
19281913

1929-
memset(handle_tlbl, 0, handle_tlbl_size * sizeof(handle_tlbl[0]));
1914+
memset(p, 0, handle_tlbl_end - (char *)p);
19301915
memset(labels, 0, sizeof(labels));
19311916
memset(relocs, 0, sizeof(relocs));
19321917

@@ -1940,24 +1925,23 @@ static void build_r3000_tlb_load_handler(void)
19401925
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
19411926
uasm_i_nop(&p);
19421927

1943-
if (p >= handle_tlbl_end)
1928+
if (p >= (u32 *)handle_tlbl_end)
19441929
panic("TLB load handler fastpath space exceeded");
19451930

19461931
uasm_resolve_relocs(relocs, labels);
19471932
pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1948-
(unsigned int)(p - handle_tlbl));
1933+
(unsigned int)(p - (u32 *)handle_tlbl));
19491934

1950-
dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_size);
1935+
dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_end);
19511936
}
19521937

19531938
static void build_r3000_tlb_store_handler(void)
19541939
{
1955-
u32 *p = handle_tlbs;
1956-
const int handle_tlbs_size = handle_tlbs_end - handle_tlbs;
1940+
u32 *p = (u32 *)handle_tlbs;
19571941
struct uasm_label *l = labels;
19581942
struct uasm_reloc *r = relocs;
19591943

1960-
memset(handle_tlbs, 0, handle_tlbs_size * sizeof(handle_tlbs[0]));
1944+
memset(p, 0, handle_tlbs_end - (char *)p);
19611945
memset(labels, 0, sizeof(labels));
19621946
memset(relocs, 0, sizeof(relocs));
19631947

@@ -1971,24 +1955,23 @@ static void build_r3000_tlb_store_handler(void)
19711955
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
19721956
uasm_i_nop(&p);
19731957

1974-
if (p >= handle_tlbs_end)
1958+
if (p >= (u32 *)handle_tlbs_end)
19751959
panic("TLB store handler fastpath space exceeded");
19761960

19771961
uasm_resolve_relocs(relocs, labels);
19781962
pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1979-
(unsigned int)(p - handle_tlbs));
1963+
(unsigned int)(p - (u32 *)handle_tlbs));
19801964

1981-
dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_size);
1965+
dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_end);
19821966
}
19831967

19841968
static void build_r3000_tlb_modify_handler(void)
19851969
{
1986-
u32 *p = handle_tlbm;
1987-
const int handle_tlbm_size = handle_tlbm_end - handle_tlbm;
1970+
u32 *p = (u32 *)handle_tlbm;
19881971
struct uasm_label *l = labels;
19891972
struct uasm_reloc *r = relocs;
19901973

1991-
memset(handle_tlbm, 0, handle_tlbm_size * sizeof(handle_tlbm[0]));
1974+
memset(p, 0, handle_tlbm_end - (char *)p);
19921975
memset(labels, 0, sizeof(labels));
19931976
memset(relocs, 0, sizeof(relocs));
19941977

@@ -2002,14 +1985,14 @@ static void build_r3000_tlb_modify_handler(void)
20021985
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
20031986
uasm_i_nop(&p);
20041987

2005-
if (p >= handle_tlbm_end)
1988+
if (p >= (u32 *)handle_tlbm_end)
20061989
panic("TLB modify handler fastpath space exceeded");
20071990

20081991
uasm_resolve_relocs(relocs, labels);
20091992
pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
2010-
(unsigned int)(p - handle_tlbm));
1993+
(unsigned int)(p - (u32 *)handle_tlbm));
20111994

2012-
dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_size);
1995+
dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_end);
20131996
}
20141997
#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
20151998

@@ -2101,12 +2084,11 @@ build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
21012084
static void build_r4000_tlb_load_handler(void)
21022085
{
21032086
u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl);
2104-
const int handle_tlbl_size = handle_tlbl_end - handle_tlbl;
21052087
struct uasm_label *l = labels;
21062088
struct uasm_reloc *r = relocs;
21072089
struct work_registers wr;
21082090

2109-
memset(handle_tlbl, 0, handle_tlbl_size * sizeof(handle_tlbl[0]));
2091+
memset(p, 0, handle_tlbl_end - (char *)p);
21102092
memset(labels, 0, sizeof(labels));
21112093
memset(relocs, 0, sizeof(relocs));
21122094

@@ -2287,25 +2269,24 @@ static void build_r4000_tlb_load_handler(void)
22872269
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
22882270
uasm_i_nop(&p);
22892271

2290-
if (p >= handle_tlbl_end)
2272+
if (p >= (u32 *)handle_tlbl_end)
22912273
panic("TLB load handler fastpath space exceeded");
22922274

22932275
uasm_resolve_relocs(relocs, labels);
22942276
pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
2295-
(unsigned int)(p - handle_tlbl));
2277+
(unsigned int)(p - (u32 *)handle_tlbl));
22962278

2297-
dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_size);
2279+
dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_end);
22982280
}
22992281

23002282
static void build_r4000_tlb_store_handler(void)
23012283
{
23022284
u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbs);
2303-
const int handle_tlbs_size = handle_tlbs_end - handle_tlbs;
23042285
struct uasm_label *l = labels;
23052286
struct uasm_reloc *r = relocs;
23062287
struct work_registers wr;
23072288

2308-
memset(handle_tlbs, 0, handle_tlbs_size * sizeof(handle_tlbs[0]));
2289+
memset(p, 0, handle_tlbs_end - (char *)p);
23092290
memset(labels, 0, sizeof(labels));
23102291
memset(relocs, 0, sizeof(relocs));
23112292

@@ -2342,25 +2323,24 @@ static void build_r4000_tlb_store_handler(void)
23422323
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
23432324
uasm_i_nop(&p);
23442325

2345-
if (p >= handle_tlbs_end)
2326+
if (p >= (u32 *)handle_tlbs_end)
23462327
panic("TLB store handler fastpath space exceeded");
23472328

23482329
uasm_resolve_relocs(relocs, labels);
23492330
pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
2350-
(unsigned int)(p - handle_tlbs));
2331+
(unsigned int)(p - (u32 *)handle_tlbs));
23512332

2352-
dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_size);
2333+
dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_end);
23532334
}
23542335

23552336
static void build_r4000_tlb_modify_handler(void)
23562337
{
23572338
u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbm);
2358-
const int handle_tlbm_size = handle_tlbm_end - handle_tlbm;
23592339
struct uasm_label *l = labels;
23602340
struct uasm_reloc *r = relocs;
23612341
struct work_registers wr;
23622342

2363-
memset(handle_tlbm, 0, handle_tlbm_size * sizeof(handle_tlbm[0]));
2343+
memset(p, 0, handle_tlbm_end - (char *)p);
23642344
memset(labels, 0, sizeof(labels));
23652345
memset(relocs, 0, sizeof(relocs));
23662346

@@ -2398,14 +2378,14 @@ static void build_r4000_tlb_modify_handler(void)
23982378
uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
23992379
uasm_i_nop(&p);
24002380

2401-
if (p >= handle_tlbm_end)
2381+
if (p >= (u32 *)handle_tlbm_end)
24022382
panic("TLB modify handler fastpath space exceeded");
24032383

24042384
uasm_resolve_relocs(relocs, labels);
24052385
pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
2406-
(unsigned int)(p - handle_tlbm));
2386+
(unsigned int)(p - (u32 *)handle_tlbm));
24072387

2408-
dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_size);
2388+
dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_end);
24092389
}
24102390

24112391
static void flush_tlb_handlers(void)

0 commit comments

Comments
 (0)