mips32_flush_cache routine corrupts CP0_STATUS with gcc-2.96
Symptom:
====
The linux mips 2.4.17 kernel compiled with gcc-2.96-110 (from H.J.Lu) hangs before reaching the 'Calibrating delay loop'. When the same kernel is compiled with gcc-3.0.4 or egcs-1.1.2 it works OK. I have included what I think is the cause, some patches to test the theory and some possible fixes.
Cause:
====
I tracked the problem back to the CP0_STATUS being corrupted by the mips32_flush_cache_all_pc routine, leading to a lockup once interrupts are enabled. Looking at a disassembly of the code suggests the broken code changes the value of the AT register while the working code leaves it alone. The compiler is allowed to do this, but it exposes the real problem which appears to be a problem between the 'cache' instruction of blast_icache() and the 'mfc0' of the __restore_flags(). The 'mfc0 at, $12' seems to be ignored. This isn't a problem with the gcc-3.0.4 code since AT still contains the value of CP0_STATUS from the __save_and_cli at the start of the routine.
This may be caused by the cache routines running from the a cached kseg0, it looks like it can be fixed by making sure that the are always called via KSEG1ADDR(fn) which looks like it could be done with a bit of fiddling of the setup_cache_funcs code. I have included a patch below which starts this, but I haven't caught all combinations of how the routines are called.
Alternatively it could be a CP0 pipeline interaction of the cache instruction and mfc0 but I can't find anything detailed about it. I thought this was the problem initially and have included a patch below which adds an extra nop.
I believe the root of the problem is that the routines are running in kseg0, but If anyone has any other ideas as to what could causing the problem then i'd be glad to know.
You can test this by inserting some extra code to change AT between the save & restore and see if it causes a problem (see included patches below)
Current source:
static inline void mips32_flush_cache_all_pc(void)
{
unsigned long flags;
__save_and_cli(flags);
blast_dcache(); blast_icache();
__restore_flags(flags);
}Disassembly of mips32_flush_cache_all_pc() for broken code gcc-2.96:00000c30 <mips32_flush_cache_all_pc>:
c30: 40066000 mfc0 a2,$12
c34: 00000000 nop
c38: 34c10001 ori at,a2,0x1
c3c: 38210001 xori at,at,0x1
c40: 40816000 mtc0 at,$12
c44: 00000040 ssnop
c48: 00000040 ssnop
c4c: 00000040 ssnop
c50: 3c030000 lui v1,0x0
c54: 8c630000 lw v1,0(v1)
c58: 3c048000 lui a0,0x8000
c5c: 3c018000 lui at,0x8000
*** See here how the compiler has changed AT here
c60: 00231821 addu v1,at,v1
c64: 0083102b sltu v0,a0,v1
c68: 10400008 beqz v0,c8c <mips32_flush_cache_all_pc+0x5c>
c6c: 00000000 nop
c70: 3c050000 lui a1,0x0
c74: 8ca50000 lw a1,0(a1)
c78: bc810000 cache 0x1,0(a0)
c7c: 00852021 addu a0,a0,a1
c80: 0083102b sltu v0,a0,v1
c84: 1440fffc bnez v0,c78 <mips32_flush_cache_all_pc+0x48>
c88: 00000000 nop
c8c: 3c030000 lui v1,0x0
c90: 8c630000 lw v1,0(v1)
c94: 3c048000 lui a0,0x8000
c98: 3c018000 lui at,0x8000
c9c: 00231821 addu v1,at,v1
ca0: 0083102b sltu v0,a0,v1
ca4: 10400008 beqz v0,cc8 <mips32_flush_cache_all_pc+0x98>
ca8: 00000000 nop
cac: 3c050000 lui a1,0x0
cb0: 8ca50000 lw a1,0(a1)
cb4: bc800000 cache 0x0,0(a0)
cb8: 00852021 addu a0,a0,a1
cbc: 0083102b sltu v0,a0,v1
cc0: 1440fffc bnez v0,cb4 <mips32_flush_cache_all_pc+0x84>
cc4: 00000000 nop
cc8: 40016000 mfc0 at,$12
*** The instruction above is the one which seems to be skipped.
ccc: 30c60001 andi a2,a2,0x1
cd0: 34210001 ori at,at,0x1
cd4: 38210001 xori at,at,0x1
cd8: 00c13025 or a2,a2,at
cdc: 40866000 mtc0 a2,$12
...
cec: 03e00008 jr ra
cf0: 00000000 nopPatches to demonstrate the problem:
====
Here is a patch to change AT in the cache_flush routine to show that this corrupts the CP0_STATUS value (for a mips32 processor with no secondary cache). When this is applied in conjunction with the patch above you should see the CP0_STATUS being corrupted and the kernel will probably hang. I have demonstrated that this change is enough to break a working kernel/compiler combination.
arch/mips/mm/c-mips32.c Wed Jul 10 14:18:17 2002 renamed from arch/mips/mm/c-mips32.c-orig Wed Jul 10 14:12:09 2002 +3 -1
@@ -74,7 +74,9 @@ unsigned long flags; __save_and_cli(flags); - blast_dcache(); blast_icache(); + blast_dcache(); + __asm__("lui\t$at, 0x8000\n\t"); + blast_icache(); __restore_flags(flags); }
Here is the patch that I used to catch the problem when CP0_STATUS is being corrupted by the cache flush routine. The CP0_STATUS should not be changed by the call to the cache flush routine.
traps.c Wed Jul 10 13:46:54 2002 renamed from arch/mips/kernel/traps.c Thu May 23 15:19:35 2002 +2 -0
@@ -889,7 +889,10 @@ memcpy((void *)(KSEG0 + 0x80), &except_vec1_generic, 0x80); memcpy((void *)(KSEG0 + 0x100), &except_vec2_generic, 0x80); memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x80); + + printk("CP0_STATUS before flush = 0x%x\n",
read_32bit_cp0_register(CP0_STATUS));
flush_icache_range(KSEG0 + 0x80, KSEG0 + 0x200);
+ printk("CP0_STATUS after flush = 0x%x\n",
read_32bit_cp0_register(CP0_STATUS));
/*
* Setup default vectors
*/Fix (to call cache routines via uncached Kseg1)
====
Assuming the root of the problem is that the cache flush routines are running from cached kseg0. This patch needs a bit more work to make sure that all the other routines are called similarly.
arch/mips/mm/c-mips32.c Wed Jul 10 14:45:03 2002 renamed from arch/mips/mm/c-mips32.c-orig Wed Jul 10 14:12:09 2002 +2 -0
@@ -606,8 +608,13 @@ { _clear_page = (void *)mips32_clear_page_dc; _copy_page = (void *)mips32_copy_page_dc; +#if 1 + _flush_cache_all = (void (*)(u32,u32)) KSEG1ADDR((unsigned
long)mips32_flush_cache_all_pc);
+ ___flush_cache_all = (void (*)(u32,u32)) KSEG1ADDR((unsigned
long)mips32_flush_cache_all_pc);
+#else
_flush_cache_all = mips32_flush_cache_all_pc;
___flush_cache_all = mips32_flush_cache_all_pc;
+#endif
_flush_cache_mm = mips32_flush_cache_mm_pc;
_flush_cache_range = mips32_flush_cache_range_pc;
_flush_cache_page = mips32_flush_cache_page_pc;Fix (If the root of the problem is a pipeline hazard)
====
The patch below fix is to insert an extra 'nop' at the end of the various blast_[id]cache routines to clear the hazard condition before the code returns. The 'sc' routines may need a similar fix. A different workaround places a 'nop' at the start of the __restore_flags routine, but I believe it is better to fix the problem at the source of the hazard.
mips32_cache.h Wed Jul 10 13:10:40 2002 renamed from include/asm-mips/mips32_cache.h Wed Apr 10 22:53:12 2002 +12 -0
@@ -189,73 +189,85 @@ static inline void blast_dcache(void) { unsigned long start = KSEG0; unsigned long end = (start + dcache_size); while(start < end) { cache_unroll(start,Index_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_dcache_page(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Hit_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_dcache_page_indexed(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Index_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache(void) { unsigned long start = KSEG0; unsigned long end = (start + icache_size); while(start < end) { cache_unroll(start,Index_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache_page(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Hit_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache_page_indexed(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Index_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_scache(void) { unsigned long start = KSEG0; unsigned long end = KSEG0 + scache_size; while(start < end) { cache_unroll(start,Index_Writeback_Inv_SD);
Jon Burgess
Quoted 5 lines · Jon Burgess
This may be caused by the cache routines running from the a cached kseg0, it looks like it can be fixed by making sure that the are always called via KSEG1ADDR(fn) which looks like it could be done with a bit of fiddling of the
setup_cache_funcscode. I have included a patch below which starts this, but I haven't caught all combinations of how the routines are called.
While that could be done it's not a good idea; running code in KSEG1 is very slow.
Quoted 3 lines
Alternatively it could be a CP0 pipeline interaction of the cache instruction and mfc0 but I can't find anything detailed about it. I thought this was the problem initially and have included a patch below which adds an extra nop.
Running uncached is so slow that the pipeline will slip and stall basically every cycle which should get you around the hazard. Anyway, there's no hazard for mfc0 documented in the MIPS32 spec so this smells like a silicon bug.
Which particular CPU and revision are you using?
Ralf
Quoted 12 lines · Ralf Baechle
Quoted 5 lines · Jon Burgess
This may be caused by the cache routines running from the a cached kseg0, it looks like it can be fixed by making sure that the are always called via KSEG1ADDR(fn) which looks like it could be done with a bit of fiddling of the
setup_cache_funcscode. I have included a patch below which starts this, but I haven't caught all combinations of how the routines are called.While that could be done it's not a good idea; running code in KSEG1 is very slow.
Unfortunately, it's required by manuals for some processors. As I know, IDT HW manuals clearly state cache flush routines must operate from uncached code and must access uncached data only. Examples are R30x1 CPU series.
Regards,
Gleb.
Quoted 10 lines · Gleb O. Raiko
Quoted 9 lines · Ralf Baechle
Quoted 5 lines · Jon Burgess
This may be caused by the cache routines running from the a cached kseg0, it looks like it can be fixed by making sure that the are always called via KSEG1ADDR(fn) which looks like it could be done with a bit of fiddling of the
setup_cache_funcscode. I have included a patch below which starts this, but I haven't caught all combinations of how the routines are called.
Quoted 8 lines
Quoted 3 lines
While that could be done it's not a good idea; running code in KSEG1 is very slow.
Unfortunately, it's required by manuals for some processors. As I know, IDT HW manuals clearly state cache flush routines must operate from uncached code and must access uncached data only. Examples are R30x1 CPU series.
Yes, that's true.
But that code belongs in the R30xx specific cache routines, not in the MIPS32 cache routines.
Quoted 3 lines
Regards,
Gleb.
--
_ _ ____ ___ Carsten Langgaard Mailto:[email protected] |\ /|||___)(___ MIPS Denmark Direct: +45 4486 5527 | \/ ||| ____) Lautrupvang 4B Switch: +45 4486 5555 TECHNOLOGIES 2750 Ballerup Fax...: +45 4486 5556 Denmark http://www.mips.com
Quoted 8 lines · Carsten Langgaard
Quoted 4 lines
Unfortunately, it's required by manuals for some processors. As I know, IDT HW manuals clearly state cache flush routines must operate from uncached code and must access uncached data only. Examples are R30x1 CPU series.
Yes, that's true.
But that code belongs in the R30xx specific cache routines, not in the MIPS32 cache routines.
I don't wonder if other IDT CPUs also require this, including those that conform MIPS32.
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
Regards,
Gleb.
Quoted 6 lines · Gleb O. Raiko
Quoted 5 lines · Carsten Langgaard
Quoted 4 lines
Unfortunately, it's required by manuals for some processors. As I know, IDT HW manuals clearly state cache flush routines must operate from uncached code and must access uncached data only. Examples are R30x1 CPU series.
Quoted 8 lines
Quoted 3 lines
Yes, that's true.
But that code belongs in the R30xx specific cache routines, not in the MIPS32 cache routines.I don't wonder if other IDT CPUs also require this, including those that conform MIPS32.
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
That could be true, but then again I suggest making specific cache routines for those CPUs.
It would be a real performance hit for the rest of us, if we have to operate from uncached space.
Quoted 3 lines
Regards,
Gleb.
--
_ _ ____ ___ Carsten Langgaard Mailto:[email protected] |\ /|||___)(___ MIPS Denmark Direct: +45 4486 5527 | \/ ||| ____) Lautrupvang 4B Switch: +45 4486 5555 TECHNOLOGIES 2750 Ballerup Fax...: +45 4486 5556 Denmark http://www.mips.com
Quoted 10 lines · Carsten Langgaard
Quoted 2 lines · Gleb O. Raiko
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
That could be true, but then again I suggest making specific cache routines for those CPUs.
It would be a real performance hit for the rest of us, if we have to operate from uncached space.
In theory, yes, there is a performance penalty. In practice, I doubt this penalty is significant. Sure, Linux likes to flush cahces, not to say more. But, did somebody measure the penalty of uncached runs? Even with microbencnmarks like lmbench.
Regards,
Gleb.
Quoted 12 lines · Gleb O. Raiko
Quoted 10 lines · Carsten Langgaard
Quoted 2 lines · Gleb O. Raiko
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
That could be true, but then again I suggest making specific cache routines for those CPUs.
It would be a real performance hit for the rest of us, if we have to operate from uncached space.
Quoted 4 lines
In theory, yes, there is a performance penalty. In practice, I doubt this penalty is significant. Sure, Linux likes to flush cahces, not to say more. But, did somebody measure the penalty of uncached runs? Even with microbencnmarks like lmbench.
Yes, I have tried running linux this way, because I wanted to eliminate the reason I sow cache problems on one of our tests chip, was due to execute the cache operating instruction from cached space.
I didn't thought it was that big a penalty, because you are flushing the cache anyway, but I didn't had to run any benchmarks, so obviously was it when I booted my system.
Quoted 3 lines
Regards,
Gleb.
--
_ _ ____ ___ Carsten Langgaard Mailto:[email protected] |\ /|||___)(___ MIPS Denmark Direct: +45 4486 5527 | \/ ||| ____) Lautrupvang 4B Switch: +45 4486 5555 TECHNOLOGIES 2750 Ballerup Fax...: +45 4486 5556 Denmark http://www.mips.com
Quoted 5 lines · Gleb O. Raiko
Quoted 2 lines
But that code belongs in the R30xx specific cache routines, not in the MIPS32 cache routines.
I don't wonder if other IDT CPUs also require this, including those that conform MIPS32.
Well, for r3k it may seem somewhat justified as cache flushing requires
cache isolation. But the IDT manual for their whole family of processors claims the D-cache can function as an I-cache (when swapped; doesn't apply when not, obviously) and cache flushing can run from KSEG0.
See "IDT MIPS Microprocessor Family Software Reference Manual", chapter 5
"Cache Management", section "Invalidation":
"To invalidate the cache in the R30xx:
[...]
The invalidate routine is normally executed with its instructions
cacheable. This sounds like a lot of trouble; but in fact shouldnt require any extra steps to run cached. An invalidation routine in uncached space will run 4-10 times slower."
That's right as the IsC bit only isolates the D-cache (either the real one or the I-cache, when swapped) and not the I-cache, so no need to waste cycles running uncached as the I-cache works normally.
Quoted 2 lines
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
Why? I see no dependency. What's the problem with interleaving cache
fills and invalidations?
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 13 lines · Maciej W. Rozycki
Quoted 2 lines · Gleb O. Raiko
I don't wonder if other IDT CPUs also require this, including those that conform MIPS32.
Well, for r3k it may seem somewhat justified as cache flushing requires
cache isolation. But the IDT manual for their whole family of processors claims the D-cache can function as an I-cache (when swapped; doesn't apply when not, obviously) and cache flushing can run from KSEG0.See "IDT MIPS Microprocessor Family Software Reference Manual", chapter 5
"Cache Management", section "Invalidation":
Quoted 7 lines
"To invalidate the cache in the R30xx:
[...]
The invalidate routine is normally executed with its instructions
cacheable. This sounds like a lot of trouble; but in fact shouldnt require any extra steps to run cached. An invalidation routine in uncached space will run 4-10 times slower."
Aha, you also stepped on this rake. :-) The problem with IDT manuals that they frequently contradict itself. You're right, SW manual allows cached flushes, but hardware manuals for the family prohibit this and state that flashes must be uncahed.
(a hw manual on family, the same chapter, the same section :-) )
It's not only the place where IDT manuals are wrong. For example, their wbflush example suggests *(int*)KSEG0 instead *(int*)KSEG1.
Quoted 5 lines
Quoted 2 lines
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
Why? I see no dependency. What's the problem with interleaving cache
fills and invalidations?
There're two possible optimization:
1. (Requires only the instruction that swaps caches must run uncached) CPU may skip implementation of double check of cache hit on loads.
Scenario: mtc0 with cache swapping with ensuring next instructions are
in cache
(pipelining here!); swap occurs; must check again the instructions are
in
the cache because the same cacheline in the data cache may have valid
bit set
and CPU will get data instead of code.
2. (Requires the whole routine must run uncached)
CPU may skip check of cache hit on loads from an isolated cache.
i don't know what optimization IDT made, perhaps, number 3. But, 1. is really worth to implement.
Regards,
Gleb.
Quoted 5 lines · Gleb O. Raiko
Aha, you also stepped on this rake. :-) The problem with IDT manuals that they frequently contradict itself. You're right, SW manual allows cached flushes, but hardware manuals for the family prohibit this and state that flashes must be uncahed.
(a hw manual on family, the same chapter, the same section :-) )
Wonderful... Add their non-existent support to that. I'm afraid I'll
have to ignore problem reports which involve their processors. :-(
Quoted 13 lines
Quoted 2 lines
Why? I see no dependency. What's the problem with interleaving cache
fills and invalidations?There're two possible optimization:
1. (Requires only the instruction that swaps caches must run uncached) CPU may skip implementation of double check of cache hit on loads.
Scenario: mtc0 with cache swapping with ensuring next instructions are
in cache
(pipelining here!); swap occurs; must check again the instructions are
in
the cache because the same cacheline in the data cache may have valid
bit set
and CPU will get data instead of code.
I can't really see a problem here for proper implementations. The CPU
may have fetched a few instructions beyond the mtc0 doing a cache swap. It's OK since we didn't modify the code. As long as the swap doesn't complete, the CPU is using the real I-cache. Once it's completed, it uses the D-cache. Since the new cache is used in the normal mode of operation, now tag matches and line replacements occur here as if it was the real I-cache. No need to do any extra checks at any stage.
Quoted 2 lines
2. (Requires the whole routine must run uncached)
CPU may skip check of cache hit on loads from an isolated cache.
But the other cache isn't isolated -- IsC only works on the cache that
plays the role of the D-cache.
Quoted 2 lines
i don't know what optimization IDT made, perhaps, number 3. But, 1. is really worth to implement.
It's possible they broke something, simply.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 12 lines
Quoted 11 lines
There're two possible optimization:
1. (Requires only the instruction that swaps caches must run uncached) CPU may skip implementation of double check of cache hit on loads.
Scenario: mtc0 with cache swapping with ensuring next instructions are
in cache
(pipelining here!); swap occurs; must check again the instructions are
in
the cache because the same cacheline in the data cache may have valid
bit set
and CPU will get data instead of code.
Quoted 2 lines
I can't really see a problem here for proper implementations. The CPU
may have fetched a few instructions beyond the mtc0 doing a cache swap.
Load from memory into I-cache, setting the valid bit.
Quoted 5 lines
It's OK since we didn't modify the code. As long as the swap doesn't complete, the CPU is using the real I-cache. Once it's completed, it uses the D-cache. Since the new cache is used in the normal mode of operation, now tag matches and line replacements occur here as if it was the real I-cache. No need to do any extra checks at any stage.
Have to check the cacheline at given address again. D-cache may have the valid bit set for the cacheline at the same address. Address means location in a cache, not memory. Check at address requires one extra tick as opposed to checking the bit.
Please, note that CPU isn't a monolitic program, but rather a set of functional blocks, so "proper implementation" may require additional signals on wires and delays.
Quoted 2 lines
It's possible they broke something, simply.
My guess they implemented No. 1. more or less.
Anybody from IDT here with strong willing to broke NDA ? :-)
Regards,
Gleb.
Quoted 4 lines · Gleb O. Raiko
Have to check the cacheline at given address again. D-cache may have the valid bit set for the cacheline at the same address. Address means location in a cache, not memory. Check at address requires one extra tick as opposed to checking the bit.
Well, you issue an instruction word read from the cache. The answer is
either a hit, providing a word at the data bus at the same time (so you can't get a hit from one cache and data from the other) or a miss with no valid data -- you have to stall in this case, waiting for a refill. Then when data from the main memory arrives, it is latched in the cache (it doesn't really matter, which one now -- if it's the wrong one, then another refill will happen next time the memory address is dereferenced) and provided to the CPU at the same time.
Quoted 3 lines
Please, note that CPU isn't a monolitic program, but rather a set of functional blocks, so "proper implementation" may require additional signals on wires and delays.
Some kind of synchronization is needed as everywhere in the CPU, as it's
mostly a synchronous circuit. As long as the state at clock edges is consistent, there is no problem with cache swapping. That's what I mean by a "proper implementation".
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 8 lines · Maciej W. Rozycki
Quoted 4 lines · Gleb O. Raiko
Have to check the cacheline at given address again. D-cache may have the valid bit set for the cacheline at the same address. Address means location in a cache, not memory. Check at address requires one extra tick as opposed to checking the bit.
Quoted 4 lines
Well, you issue an instruction word read from the cache. The answer is
either a hit, providing a word at the data bus at the same time (so you can't get a hit from one cache and data from the other) or a miss with no valid data -- you have to stall in this case, waiting for a refill.
Let it be miss and stall.
Quoted 5 lines
Then
when data from the main memory arrives, it is latched in the cache (it doesn't really matter, which one now -- if it's the wrong one, then another refill will happen next time the memory address is dereferenced) and provided to the CPU at the same time.
At this time, CPU continues the execution of previous stalled instruction. CPU knows the stalled instruction is in I-cache, but, unfortunately, caches have been swapped already. The same cacheline in the D-cache was valid bit set. CPU get data instead of code.
Regards,
Gleb.
Quoted 7 lines · Gleb O. Raiko
Quoted 4 lines
Well, you issue an instruction word read from the cache. The answer is
either a hit, providing a word at the data bus at the same time (so you can't get a hit from one cache and data from the other) or a miss with no valid data -- you have to stall in this case, waiting for a refill.Let it be miss and stall.
Quoted 7 lines
Quoted 5 lines
Then
when data from the main memory arrives, it is latched in the cache (it doesn't really matter, which one now -- if it's the wrong one, then another refill will happen next time the memory address is dereferenced) and provided to the CPU at the same time.At this time, CPU continues the execution of previous stalled
We don't care of previous instructions. The pipeline is stalled at the
intruction word fetch stage. Previously fetched instructions continue being processed until they leave the pipeline.
Quoted 3 lines
instruction. CPU knows the stalled instruction is in I-cache, but, unfortunately, caches have been swapped already. The same cacheline in the D-cache was valid bit set. CPU get data instead of code.
Well, I certainly understand what you mean, from the beginning, actually,
but I still can't see why this would happen for a real implementation. When a cache miss happens an instruction word is read directly from the main memory to the pipeline and a cache fill happens "accidentally".
What you describe, would require a CPU to query a cache status somehow
during a fill (what if another fill is in progress? -- a cache controller may perform a fill of additional lines itself as it happens in certain implementations) and then issue a second read when the fill completes. That looks weird to me -- why would you design it this way?
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 11 lines · Maciej W. Rozycki
Well, I certainly understand what you mean, from the beginning, actually,
but I still can't see why this would happen for a real implementation. When a cache miss happens an instruction word is read directly from the main memory to the pipeline and a cache fill happens "accidentally".What you describe, would require a CPU to query a cache status somehow
during a fill (what if another fill is in progress? -- a cache controller may perform a fill of additional lines itself as it happens in certain implementations) and then issue a second read when the fill completes. That looks weird to me -- why would you design it this way?
A cache is filled in cachline units. There is a possibility to fill only part of cacheline in one cache and to store the rest of data in another cache on the switch. Both caches will set the valid bit on this cacheline, but only part of cacheline is valid. In the worst cache, this operation may load instructions that will be reused later, for example, part of the main loop of the cache invalidation (sic!) routine.
Unfortunately, the behaviour depends on whether miss occurs, what instructions are loaded, how they are aligned, and so on. It means, if you get crash on this kernel version, you won't get a crash on another. If you add debug routines, everything is OK. Other black magic tricks are also here. (As you may guess, I explain my real experience here. :-). Analyzer doesn't help, bus transactions look good.)
In order to avoid this, CPU shall either perform the check again or freeze everything on the cache swap operation. The latter doesn't look real. Anyway, it's a lot of additional unnatural logic. So, the requirement to run swapping operation uncached looks reasonable.
Regards,
Gleb.
Quoted 6 lines · Gleb O. Raiko
A cache is filled in cachline units. There is a possibility to fill only part of cacheline in one cache and to store the rest of data in another cache on the switch. Both caches will set the valid bit on this cacheline, but only part of cacheline is valid. In the worst cache, this operation may load instructions that will be reused later, for example, part of the main loop of the cache invalidation (sic!) routine.
Well, it looks possible for a CPU with cache lines wider than 32-bits
(are there any such R3k-class CPUs?), indeed.
Quoted 6 lines
Unfortunately, the behaviour depends on whether miss occurs, what instructions are loaded, how they are aligned, and so on. It means, if you get crash on this kernel version, you won't get a crash on another. If you add debug routines, everything is OK. Other black magic tricks are also here. (As you may guess, I explain my real experience here. :-). Analyzer doesn't help, bus transactions look good.)
How true -- I've seen such nastinesses, too. :-/ Except that I don't
have an analyzer.
Quoted 4 lines
In order to avoid this, CPU shall either perform the check again or freeze everything on the cache swap operation. The latter doesn't look real. Anyway, it's a lot of additional unnatural logic. So, the requirement to run swapping operation uncached looks reasonable.
Well, the simplest effective approach would be a third alternative, i.e.
to make swapping happen only when no fill is in progress. Trivial logic with a single D latch on the swap signal should suffice -- I don't think the save on omitting it is worth breaking architecture specs and performance.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 2 lines · Maciej W. Rozycki
Well, it looks possible for a CPU with cache lines wider than 32-bits
(are there any such R3k-class CPUs?), indeed.
Yes, IDT R3081 has 16-byte I-cacheline. It also may have 16-byte D-cacheline, it depends on DB Refill set, may be set by wiring and in software too. DB Refill is here to support burst reads modern DRAMs have.
Quoted 10 lines
Quoted 7 lines
Unfortunately, the behaviour depends on whether miss occurs, what instructions are loaded, how they are aligned, and so on. It means, if you get crash on this kernel version, you won't get a crash on another. If you add debug routines, everything is OK. Other black magic tricks are also here. (As you may guess, I explain my real experience here. :-). Analyzer doesn't help, bus transactions look good.)
How true -- I've seen such nastinesses, too. :-/ Except that I don't
have an analyzer.
Don't care, it doesn't help in such situations.
Quoted 6 lines
Quoted 5 lines
In order to avoid this, CPU shall either perform the check again or freeze everything on the cache swap operation. The latter doesn't look real. Anyway, it's a lot of additional unnatural logic. So, the requirement to run swapping operation uncached looks reasonable.
Quoted 6 lines
Well, the simplest effective approach would be a third alternative, i.e.
to make swapping happen only when no fill is in progress. Trivial logic with a single D latch on the swap signal should suffice -- I don't think the save on omitting it is worth breaking architecture specs and performance.
In two words, it's unclear when there are no fills. Too much situations, additional stall condition (which may break spec anyway). I can't present full explanation, sorry. You have to believe. :-)
BTW, I reread my R3081 HW Manual and found two intresting places about cache operation:
"These mechanisms [cache sizing, cache flushing] are enabled through the use of the ⌠IsC■ (Isolate Cache) and SwC (Swap Cache) bits of the status register, which resides in the on-chip System Control Co-Processor (CP0). Instructions which immediately precede and succeed these operations must not be cacheable, so that the actual swapping/isolation of the cache does not disrupt operation."
Note precede instructions.
Then, on cache sizeing:
"Cache Sizing
[Famous algorithm that we implement]
Note that this software should operate as uncached. Once this algorithm is done, software should return the caches to their normal state by performing either a complete cache flush or an invalidate of those cache lines modified by the sizing algorithm."
Regards,
Gleb.
Quoted 2 lines · Gleb O. Raiko
In two words, it's unclear when there are no fills. Too much situations, additional stall condition (which may break spec anyway). I can't
The cache logic somehow must know a fill is in progress. Or at least
that it starts or finishes. Another latch may store the state.
Quoted 1 line
present full explanation, sorry. You have to believe. :-)
You don't have to convince me broken hardware is out there. I am simply
trying to emphasize there is still some demand on good hardware, even if marginally more expensive.
Quoted 9 lines
BTW, I reread my R3081 HW Manual and found two intresting places about cache operation:
"These mechanisms [cache sizing, cache flushing] are enabled through the use of the IsC (Isolate Cache) and SwC (Swap Cache) bits of the status register, which resides in the on-chip System Control Co-Processor (CP0). Instructions which immediately precede and succeed these operations must not be cacheable, so that the actual swapping/isolation of the cache does not disrupt operation."
So someone will need to code a set of cache handling functions with a
workaround for this deficiency if we are to support a system with this CPU as it appears Linux-capable.
Maciej
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 5 lines · Gleb O. Raiko
Quoted 5 lines
Quoted 4 lines
Unfortunately, it's required by manuals for some processors. As I know, IDT HW manuals clearly state cache flush routines must operate from uncached code and must access uncached data only. Examples are R30x1 CPU series.
Quoted 8 lines
Quoted 3 lines
Yes, that's true.
But that code belongs in the R30xx specific cache routines, not in the MIPS32 cache routines.I don't wonder if other IDT CPUs also require this, including those that conform MIPS32.
Basically, requirement of uncached run makes hadrware logic much simpler and allows to save silicon a bit.
The R3000 cache manipulation mechanism is implemented by giving magic meaning to store instruction while the isolate cache and swap cache bits are in use. By their very implementation they're both incompatible with normal operation of caches and therefore can only be used from uncached space.
For most part of it the situation for R4000 class CPUs is easier; a cache instruction either hits or fails. The only problem I could imagine an access conflict in the i-cache when both normal instruction fetch and the cache instruction are going to operate on the i-cache and that sounds like a less likely problem to me.
Of course not having read the RTL of all CPUs there is a bit of speculation here :)
Ralf
Quoted 5 lines · Ralf Baechle
The R3000 cache manipulation mechanism is implemented by giving magic meaning to store instruction while the isolate cache and swap cache bits are in use. By their very implementation they're both incompatible with normal operation of caches and therefore can only be used from uncached space.
Well, docs state only the cache that acts as the D-cache gets isolated
and the one that acts as the I-cache always functions normally (and the real D-cache has all the logic needed to pretend it's an I-cache successfully). Thus running from an uncached space is not needed. I haven't checked it explicitly, but the flushing functions would fail (hang) quite soon otherwise and they don't.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 7 lines
Well, docs state only the cache that acts as the D-cache gets isolated
and the one that acts as the I-cache always functions normally (and the real D-cache has all the logic needed to pretend it's an I-cache successfully). Thus running from an uncached space is not needed. I haven't checked it explicitly, but the flushing functions would fail (hang) quite soon otherwise and they don't.
I wrote the IDT software manual (I later used bits of it as the basis of parts of "See MIPS Run".) Of course, every word of it is true. I can't comment on IDT's hardware manuals, though in my experience they were somewhat better than the average.
Algorithmics produced cached routines to manipulate R3000-style caches and they work reliably; what's more, the hardware specs say they should.
Yes, it seems a bit strange to run with 'isolated'; but 'isolated' really only stops loads and stores from reading/writing at the bus interface. It's use in cache functions was an accident; the original R2000 caches did not support partial-word writes and could be invalidated just by writing a byte to them. When this was discovered to be unacceptable, the 'isolated' bit (intended for diagnostics) was pressed into service.
It is extraordinarily inefficient to run invalidate or writeback routines uncached. It will add perhaps 4-10 instruction fetches (external memory reads) for each cache line invalidated; that's probably double the memory overhead of the DMA operation which necessitated the invalidation. That's something production-quality code shouldn't do.
PS: my standard appeal. When you say you 'flush' a cache do you mean invalidate, write-back, or both? If (as I suspect) not all of you mean the same thing, should you not instead speak of 'invalidate' and 'writeback'... sloppy language surely leads to sloppy programming?
Signature · 5 lines
Dominic Sweetman Algorithmics Ltd The Fruit Farm, Ely Road, Chittering, CAMBS CB5 9PH, ENGLAND phone +44 1223 706200/fax +44 1223 706250/direct +44 1223 706205 http://www.algor.co.uk
Quoted 4 lines · Dominic Sweetman
PS: my standard appeal. When you say you 'flush' a cache do you mean invalidate, write-back, or both? If (as I suspect) not all of you mean the same thing, should you not instead speak of 'invalidate' and 'writeback'... sloppy language surely leads to sloppy programming?
I already had discussions with 68k people about this problem of terminology. It seems there is no unambigous terms for the whole ``cachology'' in the industry.
Ralf
Quoted 2 lines · Dominic Sweetman
PS: my standard appeal. When you say you 'flush' a cache do you mean invalidate, write-back, or both?
I personally mean the routine has 'flush' in its name. So, 'to flush a cache' just mens 'to call this routine'.
Considering the name belongs to the common code (as opposed to the arch specific code), I doubt the name shall be changed.
Regards,
Gleb.
Quoted 4 lines · Dominic Sweetman
PS: my standard appeal. When you say you 'flush' a cache do you mean invalidate, write-back, or both? If (as I suspect) not all of you mean the same thing, should you not instead speak of 'invalidate' and 'writeback'... sloppy language surely leads to sloppy programming?
For me, a "flush" is both (i.e., as Gleb noticed, that's what functions
with the word in names do). For a lone write back or invalidation, I would use these terms respectively.
Well, for the R3k the term is unambiguous anyway...
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
This sound more like a hardware bug to me.
What CPU are you running on ?
/Carsten
Quoted 262 lines · Jon Burgess
Symptom:
====
The linux mips 2.4.17 kernel compiled with gcc-2.96-110 (from H.J.Lu) hangs before reaching the 'Calibrating delay loop'. When the same kernel is compiled with gcc-3.0.4 or egcs-1.1.2 it works OK. I have included what I think is the cause, some patches to test the theory and some possible fixes.Cause:
====
I tracked the problem back to theCP0_STATUSbeing corrupted by themips32_flush_cache_all_pcroutine, leading to a lockup once interrupts are enabled. Looking at a disassembly of the code suggests the broken code changes the value of the AT register while the working code leaves it alone. The compiler is allowed to do this, but it exposes the real problem which appears to be a problem between the 'cache' instruction ofblast_icache()and the 'mfc0' of the__restore_flags(). The 'mfc0 at, $12' seems to be ignored. This isn't a problem with the gcc-3.0.4 code since AT still contains the value ofCP0_STATUSfrom the__save_and_cliat the start of the routine.This may be caused by the cache routines running from the a cached kseg0, it looks like it can be fixed by making sure that the are always called via KSEG1ADDR(fn) which looks like it could be done with a bit of fiddling of the
setup_cache_funcscode. I have included a patch below which starts this, but I haven't caught all combinations of how the routines are called.Alternatively it could be a CP0 pipeline interaction of the cache instruction and mfc0 but I can't find anything detailed about it. I thought this was the problem initially and have included a patch below which adds an extra nop.
I believe the root of the problem is that the routines are running in kseg0, but If anyone has any other ideas as to what could causing the problem then i'd be glad to know.
You can test this by inserting some extra code to change AT between the save & restore and see if it causes a problem (see included patches below)
Current source:
static inline voidmips32_flush_cache_all_pc(void){ unsigned long flags; __save_and_cli(flags); blast_dcache(); blast_icache(); __restore_flags(flags); }Disassembly of
mips32_flush_cache_all_pc()for broken code gcc-2.96:00000c30<mips32_flush_cache_all_pc>:c30: 40066000 mfc0 a2,$12 c34: 00000000 nop c38: 34c10001 ori at,a2,0x1 c3c: 38210001 xori at,at,0x1 c40: 40816000 mtc0 at,$12 c44: 00000040 ssnop c48: 00000040 ssnop c4c: 00000040 ssnop c50: 3c030000 lui v1,0x0 c54: 8c630000 lw v1,0(v1) c58: 3c048000 lui a0,0x8000 c5c: 3c018000 lui at,0x8000 *** See here how the compiler has changed AT here c60: 00231821 addu v1,at,v1 c64: 0083102b sltu v0,a0,v1 c68: 10400008 beqz v0,c8c <mips32_flush_cache_all_pc+0x5c> c6c: 00000000 nop c70: 3c050000 lui a1,0x0 c74: 8ca50000 lw a1,0(a1) c78: bc810000 cache 0x1,0(a0) c7c: 00852021 addu a0,a0,a1 c80: 0083102b sltu v0,a0,v1 c84: 1440fffc bnez v0,c78 <mips32_flush_cache_all_pc+0x48> c88: 00000000 nop c8c: 3c030000 lui v1,0x0 c90: 8c630000 lw v1,0(v1) c94: 3c048000 lui a0,0x8000 c98: 3c018000 lui at,0x8000 c9c: 00231821 addu v1,at,v1 ca0: 0083102b sltu v0,a0,v1 ca4: 10400008 beqz v0,cc8 <mips32_flush_cache_all_pc+0x98> ca8: 00000000 nop cac: 3c050000 lui a1,0x0 cb0: 8ca50000 lw a1,0(a1) cb4: bc800000 cache 0x0,0(a0) cb8: 00852021 addu a0,a0,a1 cbc: 0083102b sltu v0,a0,v1 cc0: 1440fffc bnez v0,cb4 <mips32_flush_cache_all_pc+0x84> cc4: 00000000 nop cc8: 40016000 mfc0 at,$12 *** The instruction above is the one which seems to be skipped. ccc: 30c60001 andi a2,a2,0x1 cd0: 34210001 ori at,at,0x1 cd4: 38210001 xori at,at,0x1 cd8: 00c13025 or a2,a2,at cdc: 40866000 mtc0 a2,$12 ... cec: 03e00008 jr ra cf0: 00000000 nopPatches to demonstrate the problem:
====
Here is a patch to change AT in thecache_flushroutine to show that this corrupts theCP0_STATUSvalue (for a mips32 processor with no secondary cache). When this is applied in conjunction with the patch above you should see theCP0_STATUSbeing corrupted and the kernel will probably hang. I have demonstrated that this change is enough to break a working kernel/compiler combination.arch/mips/mm/c-mips32.c Wed Jul 10 14:18:17 2002 renamed from arch/mips/mm/c-mips32.c-orig Wed Jul 10 14:12:09 2002 +3
-1@@ -74,7 +74,9 @@ unsigned long flags; __save_and_cli(flags); - blast_dcache(); blast_icache(); + blast_dcache(); + __asm__("lui\t$at, 0x8000\n\t"); + blast_icache(); __restore_flags(flags); }Here is the patch that I used to catch the problem when
CP0_STATUSis being corrupted by the cache flush routine. TheCP0_STATUSshould not be changed by the call to the cache flush routine.traps.c Wed Jul 10 13:46:54 2002 renamed from arch/mips/kernel/traps.c Thu May 23 15:19:35 2002 +3
-0@@ -889,7 +889,10 @@ memcpy((void *)(KSEG0 + 0x80), &except_vec1_generic, 0x80); memcpy((void *)(KSEG0 + 0x100), &except_vec2_generic, 0x80); memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x80); + + printk("CP0_STATUS before flush = 0x%x\n", read_32bit_cp0_register(CP0_STATUS)); flush_icache_range(KSEG0 + 0x80, KSEG0 + 0x200); + printk("CP0_STATUS after flush = 0x%x\n", read_32bit_cp0_register(CP0_STATUS)); /** Setup default vectors
*/Fix (to call cache routines via uncached Kseg1)
====
Assuming the root of the problem is that the cache flush routines are running from cached kseg0. This patch needs a bit more work to make sure that all the other routines are called similarly.arch/mips/mm/c-mips32.c Wed Jul 10 14:45:03 2002 renamed from arch/mips/mm/c-mips32.c-orig Wed Jul 10 14:12:09 2002 +5
-0@@ -606,8 +608,13 @@ { _clear_page = (void *)mips32_clear_page_dc; _copy_page = (void *)mips32_copy_page_dc; +#if 1 + _flush_cache_all = (void (*)(u32,u32)) KSEG1ADDR((unsigned long)mips32_flush_cache_all_pc); + ___flush_cache_all = (void (*)(u32,u32)) KSEG1ADDR((unsigned long)mips32_flush_cache_all_pc); +#else _flush_cache_all = mips32_flush_cache_all_pc; ___flush_cache_all = mips32_flush_cache_all_pc; +#endif _flush_cache_mm = mips32_flush_cache_mm_pc;_flush_cache_range = mips32_flush_cache_range_pc; _flush_cache_page = mips32_flush_cache_page_pc;Fix (If the root of the problem is a pipeline hazard)
====
The patch below fix is to insert an extra 'nop' at the end of the various blast_[id]cache routines to clear the hazard condition before the code returns. The 'sc' routines may need a similar fix. A different workaround places a 'nop' at the start of the__restore_flagsroutine, but I believe it is better to fix the problem at the source of the hazard.mips32_cache.h Wed Jul 10 13:10:40 2002 renamed from include/asm-mips/mips32_cache.h Wed Apr 10 22:53:12 2002 +12
-0@@ -189,73 +189,85 @@ static inline void blast_dcache(void) { unsigned long start = KSEG0; unsigned long end = (start + dcache_size); while(start < end) { cache_unroll(start,Index_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_dcache_page(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Hit_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_dcache_page_indexed(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Index_Writeback_Inv_D); start += dc_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache(void) { unsigned long start = KSEG0; unsigned long end = (start + icache_size); while(start < end) { cache_unroll(start,Index_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache_page(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Hit_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_icache_page_indexed(unsigned long page) { unsigned long start = page; unsigned long end = (start + PAGE_SIZE); while(start < end) { cache_unroll(start,Index_Invalidate_I); start += ic_lsize; } + /* Prevent hazard with following mfc0 */ + __asm__("nop\n\t"); } static inline void blast_scache(void) { unsigned long start = KSEG0; unsigned long end = KSEG0 + scache_size; while(start < end) { cache_unroll(start,Index_Writeback_Inv_SD);
Quoted 1 line
Jon Burgess
--
_ _ ____ ___ Carsten Langgaard Mailto:[email protected] |\ /|||___)(___ MIPS Denmark Direct: +45 4486 5527 | \/ ||| ____) Lautrupvang 4B Switch: +45 4486 5555 TECHNOLOGIES 2750 Ballerup Fax...: +45 4486 5556 Denmark http://www.mips.com