Skip to contentSkip to conversation

Virtual memory size detection for 64 bit MIPS CPUs

PATCH Superseded Guenter Roeck 3 participants 6 messages Feb 1, 2010 MIPS

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Virtual memory size detection for 64 bit MIPS CPUs

Linux kernel 2.6.32 and later allocates memory from the top of virtual memory space.

This patch implements virtual memory size detection for 64 bit MIPS CPUs to avoid resulting crashes.

---
 arch/mips/include/asm/cpu-features.h |    3 +++
 arch/mips/include/asm/cpu-info.h     |    1 +
 arch/mips/include/asm/pgtable-64.h   |    4 +++-
 arch/mips/kernel/cpu-probe.c         |   22 ++++++++++++++++++++++
 4 files changed, 29 insertions(+), 1 deletions(-)
arch/mips/include/asm/cpu-features.h +3 -0
index 1f4df64..284eb55 100644
@@ -209,6 +209,9 @@
 # ifndef cpu_has_64bit_addresses
 # define cpu_has_64bit_addresses	1
 # endif
+# ifndef cpu_vmbits
+# define cpu_vmbits cpu_data[0].vmbits
+# endif
 #endif
 
 #if defined(CONFIG_CPU_MIPSR2_IRQ_VI) && !defined(cpu_has_vint)
arch/mips/include/asm/cpu-info.h +1 -0
index 1260443..3c694bc 100644
@@ -58,6 +58,7 @@ struct cpuinfo_mips {
 	struct cache_desc	tcache;	/* Tertiary/split secondary cache */
 	int			srsets;	/* Shadow register sets */
 	int			core;	/* physical core number */
+	int			vmbits;	/* Virtual memory size in bits */
 #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
 	/*
 	 * In the MIPS MT "SMTC" model, each TC is considered
arch/mips/include/asm/pgtable-64.h +3 -1
index 9cd5089..259ec58 100644
@@ -110,7 +110,9 @@
 #define VMALLOC_START		MAP_BASE
 #define VMALLOC_END	\
 	(VMALLOC_START + \
-	 PTRS_PER_PGD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE - (1UL << 32))
+	 min(PTRS_PER_PGD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, \
+	     (1UL<<cpu_vmbits)) - (1UL << 32))
+
 #if defined(CONFIG_MODULES) && defined(KBUILD_64BIT_SYM32) && \
 	VMALLOC_START != CKSSEG
 /* Load modules into 32bit-compatible segment. */
arch/mips/kernel/cpu-probe.c +22 -0
index 7a51866..1286b0e 100644
@@ -282,6 +282,26 @@ static inline int __cpu_has_fpu(void)
 	return ((cpu_get_fpu_id() & 0xff00) != FPIR_IMP_NONE);
 }
 
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
+			     ".set mips0\n"
+			     : "=r" (zbits));
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+
 #define R4K_OPTS (MIPS_CPU_TLB | MIPS_CPU_4KEX | MIPS_CPU_4K_CACHE \
 		| MIPS_CPU_COUNTER)
 
@@ -967,6 +987,8 @@ __cpuinit void cpu_probe(void)
 		c->srsets = ((read_c0_srsctl() >> 26) & 0x0f) + 1;
 	else
 		c->srsets = 1;
+
+	cpu_set_vmbits(c);
 }
 
 __cpuinit void cpu_report(void)
Signature · 1 line
1.6.0.4
#1 lore raw

Guenter Roeck wrote:
[...]

Quoted 11 lines
arch/mips/kernel/cpu-probe.c +10 -0
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"

arch/mips/kernel/cpu-probe.c static inline int __cpu_has_fpu(void) · line 304

Quoted 10 lines
arch/mips/kernel/cpu-probe.c +10 -0
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
+			     ".set mips0\n"
+			     : "=r" (zbits));
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+

It should be possible to express this in 'pure' C using read_c0_entryhi()/write_c0_entryhi(), also you need to be sure you are not writing 1s to any reserved bits of the register.

David Daney

#2 lore raw
Quoted 18 lines · David Daney

Guenter Roeck wrote:
[...]

Quoted 16 lines
arch/mips/kernel/cpu-probe.c +15 -0
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
+			     ".set mips0\n"
+			     : "=r" (zbits));
Quoted 8 lines

arch/mips/kernel/cpu-probe.c static inline int __cpu_has_fpu(void) · line 304

Quoted 5 lines
arch/mips/kernel/cpu-probe.c +5 -0
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+

It should be possible to express this in 'pure' C using read_c0_entryhi()/write_c0_entryhi(), also you need to be sure you are

Sure, no problem.

Quoted 2 lines

not writing 1s to any reserved bits of the register.

That may be tricky, since the upper bits are reserved in some architectures. For example, the 20Kc core specification says that bits 61:40 are reserved and must be written with 0.

I can write, say, 0x3fffffffffff0000 to avoid writing into lower reserved bits, but that won't help for any upper reserved bits. Would that be acceptable / better ?

Guenter

#3 lore raw
Quoted 30 lines · Guenter Roeck
Quoted 16 lines · David Daney

Guenter Roeck wrote:
[...]

Quoted 14 lines
arch/mips/kernel/cpu-probe.c +13 -0
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
Quoted 8 lines

arch/mips/kernel/cpu-probe.c static inline int __cpu_has_fpu(void) · line 304

Quoted 7 lines
arch/mips/kernel/cpu-probe.c +7 -0
+			     ".set mips0\n"
+			     : "=r" (zbits));
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+

It should be possible to express this in 'pure' C using read_c0_entryhi()/write_c0_entryhi(), also you need to be sure you are

Sure, no problem.

Quoted 2 lines

not writing 1s to any reserved bits of the register.

Quoted 7 lines

That may be tricky, since the upper bits are reserved in some architectures. For example, the 20Kc core specification says that bits 61:40 are reserved and must be written with 0.

I can write, say, 0x3fffffffffff0000 to avoid writing into lower reserved bits, but that won't help for any upper reserved bits. Would that be acceptable / better ?

The MIPS64® Privileged Resource Architecture manual might be a better reference.

I would set 1s only in bits 13-63 (the VPN2 and R fields). We don't support and don't really care about VPN2X, the upper bits are really the only ones of interest here.

David Daney

#4 lore raw
Quoted 36 lines · David Daney
Quoted 35 lines · Guenter Roeck
Quoted 17 lines · David Daney

Guenter Roeck wrote:
[...]

Quoted 15 lines
arch/mips/kernel/cpu-probe.c +14 -0
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
+			     ".set mips0\n"
Quoted 8 lines

arch/mips/kernel/cpu-probe.c static inline int __cpu_has_fpu(void) · line 304

Quoted 6 lines
arch/mips/kernel/cpu-probe.c +6 -0
+			     : "=r" (zbits));
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+

It should be possible to express this in 'pure' C using read_c0_entryhi()/write_c0_entryhi(), also you need to be sure you are

Sure, no problem.

Quoted 2 lines

not writing 1s to any reserved bits of the register.

That may be tricky, since the upper bits are reserved in some architectures. For example, the 20Kc core specification says that bits 61:40 are reserved and must be written with 0.

Quoted 8 lines
Quoted 3 lines

I can write, say, 0x3fffffffffff0000 to avoid writing into lower reserved bits, but that won't help for any upper reserved bits. Would that be acceptable / better ?

The MIPS64® Privileged Resource Architecture manual might be a better reference.

Unfortunately, the download link for that manual on mips.com seems to be broken.

Quoted 4 lines

I would set 1s only in bits 13-63 (the VPN2 and R fields). We don't support and don't really care about VPN2X, the upper bits are really the only ones of interest here.

Ok, I'll do that.

This is the new code:

static inline void cpu_set_vmbits(struct cpuinfo_mips *c)

{
        if (cpu_has_64bits) {
                write_c0_entryhi(0xfffffffffffff000ULL);
                c->vmbits = __fls(read_c0_entryhi() &
0x3ffffffffffff000ULL) + 1;
        } else
                c->vmbits = 32;
}

Guenter

#5 lore raw
In reply to Guenter Roeck
Quoted 28 lines · Guenter Roeck
Quoted 18 lines · David Daney

Guenter Roeck wrote:
[...]

Quoted 16 lines
arch/mips/kernel/cpu-probe.c +15 -0
+static inline void cpu_set_vmbits(struct cpuinfo_mips *c)
+{
+	if (cpu_has_64bits) {
+		unsigned long zbits;
+
+		asm volatile(".set mips64\n"
+			     "and %0, 0\n"
+			     "dsubu %0, 1\n"
+			     "dmtc0 %0, $10, 0\n"
+			     "dmfc0 %0, $10, 0\n"
+			     "dsll %0, %0, 2\n"
+			     "dsra %0, %0, 2\n"
+			     "dclz %0, %0\n"
+			     ".set mips0\n"
+			     : "=r" (zbits));
Quoted 8 lines

arch/mips/kernel/cpu-probe.c static inline int __cpu_has_fpu(void) · line 304

Quoted 5 lines
arch/mips/kernel/cpu-probe.c +5 -0
+		c->vmbits = 64 - zbits;
+	} else
+		c->vmbits = 32;
+}
+

It should be possible to express this in 'pure' C using read_c0_entryhi()/write_c0_entryhi(), also you need to be sure you are

Quoted 1 line

Sure, no problem.

Especially as:

1. DCLZ is not a valid MIPS III instruction; some 64-bit CPUs will fault on it.

2. You have to take care of CP0 hazards, e.g. with the R4000 if an MTC0 is immediately followed by an MFC0 accessing the same CP0 register, then the result of the latter instruction is unpredictable.

Maciej