MIPS CP0 Random register fix
PATCH 1/1 MIPS CP0 Random register fix
Hello,
MIPS TLBWR instruction asks the CPU to randomly overwrite a TLB entry by the one we want to write. The TLB index needs to be between number of wired TLB entries and TLB count - 1. However, algorithm to choose which one to overwrite is implementation dependant.
At the moment, Qemu implementation is a random one, but can return the same value more than once. Due to this, NetBSD 1.6.2 on MIPS Magnum emulation crashes.
After checking MIPS CPU documentations, multiple algorithms exist to update this Random register, but they all guarantee that 2 close TLBWR instructions don't overwrite the same TLB.
Attached patch prevents returning the same TLB index twice, by choosing the next immediate value if random value is the same as before.
Hervé
mips_timer.c +8 -0
@@ -8,9 +8,17 @@uint32_t cpu_mips_get_random (CPUState *env) {static uint32_t seed =0 ; +static uint32_t prev_idx =0 ;uint32_t idx; seed = seed *314159 +1 ; idx = (seed >>16 ) % (env->tlb->nb_tlb - env->CP0_Wired) + env->CP0_Wired; +if (idx == prev_idx) { +/* Don't return same value twice, so get another value */ + idx = prev_idx +1 ; +if (idx == env->tlb->nb_tlb) + idx = env->CP0_Wired; + } + prev_idx = idx;return idx; }
Quoted 12 lines · Hervé Poussineau
Hello,
MIPS TLBWR instruction asks the CPU to randomly overwrite a TLB entry by the one we want to write. The TLB index needs to be between number of wired TLB entries and TLB count - 1.
However, algorithm to choose which one to overwrite is implementation dependant.At the moment, Qemu implementation is a random one, but can return the same value more than once.
Due to this, NetBSD 1.6.2 on MIPS Magnum emulation crashes.
Quoted 6 lines
After checking MIPS CPU documentations, multiple algorithms exist to update this Random register, but they all guarantee that 2 close TLBWR instructions don't overwrite the same TLB.
Attached patch prevents returning the same TLB index twice, by choosing the next immediate value if random value is the same as before.
I have actually applied a different patch, which also change to a better random generator. PLease confirm that it works.
Signature · 4 lines
.''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net