0

I want to verify my stw actually wrote to a memory location using C.

I know I could use a load word to read it(as long as I didn't use the same register it was written to?), but what's the equivalent syntax that would work in C?

JWorkin
  • 31
  • 5
  • What is the address you are writing to with `stw`? How is the C program supposed to know that address? Are you concerned with whether the data was actually written to physical memory, not just cache? – Eric Postpischil Mar 26 '21 at 18:54
  • @EricPostpischil see above for clarification. It's all under the same project. I will know the address of where the written data resides. The data is written to cache and I need to make sure "the assembly write" actually was written to cache. – JWorkin Mar 26 '21 at 19:02
  • Your inline asm is missing a `"memory"` clobber: C access to it can assume that the asm statement didn't change it. (https://stackoverflow.com/tags/inline-assembly/info) You also forgot to tell the compiler about registers you modify, including `"cc"` condition codes, if any of those PowerPC(?) instructions do that. – Peter Cordes Mar 26 '21 at 19:03
  • @PeterCordes it is PowerPC – JWorkin Mar 26 '21 at 19:05
  • @PeterCordes I'll have to look up what clobber does.. I'm fuzzy with assembly, but I've included more of the code above because I wasn't sure if it was relevant or not, which may or may not be what you were referencing. – JWorkin Mar 26 '21 at 19:09
  • Yes, you can't just modify memory without telling the compiler about it, or even read it: [How can I indicate that the memory \*pointed\* to by an inline ASM argument may be used?](https://stackoverflow.com/q/56432259). And definitely not registers, `"r"(tAddress)` tell the compiler this is a read-only input, so it will assume that the value in the register it picks hasn't changed. Use `"+r"(tAddress)` if you want a read-write operand you can increment. (Inside a wrapper function, it won't matter that you modify `tAddress` because it's the function's private C var.) – Peter Cordes Mar 26 '21 at 19:12
  • @PeterCordes Are you saying this write... isn't actually writing at all? and the compiler thinks it is read-only? – JWorkin Mar 26 '21 at 19:21
  • No, I'm saying the compiler might make false assumptions because your constraints don't accurately describe the asm to it. This can lead to unpredictable results because it's Undefined Behaviour. Look at the compiler's asm output to see what actually happened, and how the compiler generated asm gets mixed with your inline asm. – Peter Cordes Mar 26 '21 at 19:29
  • @PeterCordes gotcha. tAddress is basically the target address. – JWorkin Mar 26 '21 at 19:55

1 Answers1

2

You can read from an arbitrary memory address like this:

volatile uint64_t * const addr = (volatile uint64_t * const) 0x12345678;

printf("%llx\n",*addr);

Replace uint64_t with the word size of your system.

This will need to be run in the same program as your assembly routine which does the stw, so that you are writing/reading the same virtual address in the same virtual address space.

Nola
  • 436
  • 4
  • 12