aarch64 MMU translation table - arm64

I've been experimenting with MMU on RPi3B+ with no success at all. I tried following official ARM examples, and I generate tables like this:
Level 2 table is at 1016000 PA:
0000000001016000: 0000000000000405 0000000000200405 0000000000400405 0000000000600405
0000000001016020: 0000000000800405 0000000000A00405 0000000000C00405 0000000000E00405
0000000001016040: 0000000001000405 0000000001200405 0000000001400405 0000000001600405
...
0000000001016FC0: 006000003F000601 006000003F200601 006000003F400601 006000003F600601
0000000001016FE0: 006000003F800601 006000003FA00601 006000003FC00601 006000003FE00601
Level 1 table is at 1014000:
0000000001014000: 0000000001016003 0000000000000000 0000000000000000 0000000000000000
0000000001014020: 0000000000000000 0000000000000000 0000000000000000 0000000000000000
...
0000000001014FE0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000
The only entry in Level 1 table is the one that points to a Level 2 table.
MAIR: 0x000000000044FF00
TCR: 0x0000000000803520
Anyway, when I enable MMU, I lose any other information, as UART does not work anymore. This should be 1:1 mapping in EL1. I tried experimenting with both 32bits and 39bits VA space in TCR. Also with enabling ttbr1, nothing works at all. What am I doing wrong?
Edit: This works perfectly in qemu-system-aarch64 -M raspi3, but not on device when using U-Boot. Also it does not work if loaded directly in RPi (at 0x80000). Any explanation as to why is welcome.

It seems the problem was somewhere in my code for enabling / disabling MMU. Because I wanted to keep the same logic in early start and when enabling MMU, I wrote this macro:
.macro set_mmu_el el enabled
mov x0, xzr
ldr x2, =\enabled
lsl x1, x2, #12
orr x0, x0, x1
lsl x1, x2, #2
orr x0, x0, x1
orr x0, x0, x2
msr sctlr_el\el, x0
.endm
Then, this was called in:
.globl mmu_enable_el1
mmu_enable_el1:
sub sp, sp, #0x16
str x30, [sp]
dsb ish
isb
set_mmu_el 1 _MMU_ENABLED
isb
nop
nop
nop
nop
ldr x30, [sp]
add sp, sp, #0x16
ret
And again, this was called in:
void mmu_init() {
// ...
mmu_enable_el1();
}
I replaced this with only:
asm volatile("dsb ish; isb; msr sctlr_el1, %0; isb; nop; nop; nop; nop"::"r"(0x000005 | (1 << 12)));
Now it works perfectly, with both D-cache and I-cache enabled. I am not sure what went wrong with the code above, but anyway, a lesson has been learned. At least my page table format was correct.

Related

what does Spill mean in ARM ISA

I have write some test code, just for testing the stack overflow.
int MyFunc(long c)
{
char buffer[12];
return sprintf(buffer, "xxxxxxxxxxx%ld", c);
}
when I compile it to ARM64 assembly, I got the result
MyFunc(long): // #MyFunc(long)
sub sp, sp, #32 // =32
stp x29, x30, [sp, #16] // 16-byte Folded Spill
add x29, sp, #16 // =16
adrp x1, .L.str
mov x2, x0
add x1, x1, :lo12:.L.str
add x0, sp, #4 // =4
bl sprintf
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
add sp, sp, #32 // =32
ret
so, what does 16-byte Folded Spill mean? and why?
Stackoverflow.com is the perfect place for stack spill questions. =)
16-byte is because we are saving two 8-byte registers to the stack.
Folded for this one I am going to have to guess. Folding is when math operations are simplified combined. I going to go out on a whim and say it does not apply here and that it’s just a generic compiler message that only sometimes applies. If we had several of these in a row to save many registers to the stack and incremented the stack pointer(sp) just once then it would be combined because we would be using -32+16=-16 and this does not cause any extra math.
sub sp, sp, #32
stp x27, x28, [sp]
stp x29, x30, [sp, #16] <--- folded because we decrement sp once on 1st row
Spill is the process of saving registers to memory because we need the extra registers. This is often referred to as spilling the registers to memory.

How do I solve SIGABRT "Abort with payload" with no error message?

I am very new to Swift so I apologise if my question may sound obvious, but I looked everywhere and could not find any solutions for my issue. I know there are other similar questions to mine, it is really not my intention to make a duplicate, but I believe I needed to make a new question because none of the answers to said similar questions have solved my problem, it appears to me to be a different kind of issue and I would love to receive some help.
I have been trying to develop this app which has been running just fine in the simulator in the past few weeks. However, I need to try it out on my iOS device to try some features that involve the use of a camera. The problem is that the app crashes on launch every single time. I tried to download another app from GitHub, a simple one that doesn't involve the use of pods, and it runs fine on the same iOS device. It's just my app that doesn't. Here's the error on crash:
dyld`__abort_with_payload:
0x102dab5dc <+0>: mov x16, #0x209
0x102dab5e0 <+4>: svc #0x80
-> 0x102dab5e4 <+8>: b.lo 0x102dab600 ; <+36>
0x102dab5e8 <+12>: stp x29, x30, [sp, #-0x10]!
0x102dab5ec <+16>: mov x29, sp
0x102dab5f0 <+20>: bl 0x102da9be8 ; cerror_nocancel
0x102dab5f4 <+24>: mov sp, x29
0x102dab5f8 <+28>: ldp x29, x30, [sp], #0x10
0x102dab5fc <+32>: ret
0x102dab600 <+36>: ret
dyld`_dyld_start:
0x102d61000 <+0>: mov x28, sp
0x102d61004 <+4>: and sp, x28, #0xfffffffffffffff0
0x102d61008 <+8>: mov x0, #0x0
0x102d6100c <+12>: mov x1, #0x0
0x102d61010 <+16>: stp x1, x0, [sp, #-0x10]!
0x102d61014 <+20>: mov x29, sp
0x102d61018 <+24>: sub sp, sp, #0x10 ; =0x10
0x102d6101c <+28>: ldr x0, [x28]
0x102d61020 <+32>: ldr x1, [x28, #0x8]
0x102d61024 <+36>: add x2, x28, #0x10 ; =0x10
0x102d61028 <+40>: adrp x3, -1
0x102d6102c <+44>: add x3, x3, #0x0 ; =0x0
0x102d61030 <+48>: mov x4, sp
0x102d61034 <+52>: bl 0x102d6107c ; dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*)
-> 0x102d61038 <+56>: mov x16, x0
0x102d6103c <+60>: ldr x1, [sp]
0x102d61040 <+64>: cmp x1, #0x0 ; =0x0
0x102d61044 <+68>: b.ne 0x102d61050 ; <+80>
0x102d61048 <+72>: add sp, x28, #0x8 ; =0x8
0x102d6104c <+76>: br x16
0x102d61050 <+80>: mov x30, x1
0x102d61054 <+84>: ldr x0, [x28, #0x8]
0x102d61058 <+88>: add x1, x28, #0x10 ; =0x10
0x102d6105c <+92>: add x2, x1, x0, lsl #3
0x102d61060 <+96>: add x2, x2, #0x8 ; =0x8
0x102d61064 <+100>: mov x3, x2
0x102d61068 <+104>: ldr x4, [x3]
0x102d6106c <+108>: add x3, x3, #0x8 ; =0x8
0x102d61070 <+112>: cmp x4, #0x0 ; =0x0
0x102d61074 <+116>: b.ne 0x102d61068 ; <+104>
0x102d61078 <+120>: br x16
Here's a picture of the error.
I have been searching all over the internet for this question and I've tried all the possible solutions, but with no success. Again, perhaps it's because of my lack of experience... but here's what I have tried so far:
I added the frameworks in the tab target > general, with no success.
Here's a picture.
I changed the frameworks' status to "Optional" in the tab build phases > link binary with libraries.
Here's a picture of that, too.
If I change it to "embed & sign", it gives me multiple errors like these, one for each framework:
Both red,
and yellow errors.
If I go to run > diagnostics, guard malloc & thread sanitizer are already unticked:
Here's my stats.
At one point, I was desperate enough to try and delete all the frameworks files lol (I obviously did so in a duplicate project) and it was disastrous.
The trust settings of my certificates in the keychain are all set to System Defaults.
My info.plist does have "Privary - Camera Usage Description", type String.
If it makes any difference, I use pods for Firebase and IQKeyboardManagerSwift. I say this because I noticed how most of the custom frameworks have the name of the pods (don't laugh at me please, I literally started coding like last month lol!). However one thing that isn't really clear to me is why these frameworks are greyed out in the Navigator, maybe that could be the problem? Again, I have no idea... plus, I have some frameworks in the pods section (see below), and at least a hundred in the app section, below the "info.plist" file and the "GoogleService-Info.plist" file. Is that even normal?
Frameworks in the app section;
Frameworks in the pods section.
I think I said a lot but if any information is missing please let me know. I want to thank everybody in advance for any help you may be able to provide, I am so desperate to solve this!
PS: I am not even allowed to enter images yet, I hope you can check the links, though!
I totally forgot to update this, but I solved by updating my phone to 13.4 (which is still a beta version) and my xcode too (also beta version). It seems that the problem is the previous/current update, not sure why but it solved the problem for me. Hope it can save someone else some headache.

"Project Name" was compiled with optimization

I create a game with Unity for iOS.
When Xcode upgrades to 9.3 , sometimes the project crashes with this error when I'm in the game:
was compiled with optimization - stepping may behave oddly; variables
may not be available.
I searched and find many ways to fix this problem, but i still get it.
I didn't use optimization and i uncheck the code strip in unity setting.
any idea how can i fix it?!
UPDATE:
Another informat
ShouldUpdateTransformBefore():
0x100f9c064 <+0>: ldr x8, [x0]
0x100f9c068 <+4>: ldr x9, [x1]
0x100f9c06c <+8>: cmp x8, x9
0x100f9c070 <+12>: b.eq 0x100f9c07c ; <+24> at RectTransform.cpp:319
0x100f9c074 <+16>: cset w0, lo
0x100f9c078 <+20>: ret
0x100f9c07c <+24>: ldr w9, [x1, #0x8]
0x100f9c080 <+28>: cmn w9, #0x1 ; =0x1
0x100f9c084 <+32>: b.eq 0x100f9c0a4 ; <+64> at RectTransform.cpp:320
0x100f9c088 <+36>: ldr w10, [x0, #0x8]
0x100f9c08c <+40>: cmp w9, w10
0x100f9c090 <+44>: b.eq 0x100f9c0ac ; <+72> at RectTransform.cpp:327
-> 0x100f9c094 <+48>: ldr x11, [x8, #0x10]
0x100f9c098 <+52>: ldr w9, [x11, w9, sxtw #2]
0x100f9c09c <+56>: cmn w9, #0x1 ; =0x1
0x100f9c0a0 <+60>: b.ne 0x100f9c08c ; <+40> at RectTransform.cpp:322
0x100f9c0a4 <+64>: mov w0, #0x0
0x100f9c0a8 <+68>: ret
0x100f9c0ac <+72>: orr w0, wzr, #0x1
0x100f9c0b0 <+76>: ret
Although seems it is too late, I just face similar problem and share the solution here.
Reason
In my case, the application using too much memories causes the testing device crashes. You can test it with a better device. My application crashes in iPhone7 but not in iPhone7 Plus. And you can also check it in the Memory column in the left.
Solution
If application crashes when loading scene, here is the solution, or suggestion.
Assume there are 2 scenes, A and B. Both of them cost 1GB memory.
When A -> B, A will be destroyed after B is loaded, which means 2GB is requested when A + B. It is too heavy for mobile.
So I add scene C, a middleware cost 0.1GB memory.
And now I go to scene B through A -> C -> B.
A + C request 1.1GB, then Scene A releases.
After that, C -> B request 1.1GB.
The maximum memory of loading scene from A to B decrease from 2GB to 1.1GB. It solved my application crashing problem. Hope it helps.

ARM NEON code in Xcode Project - iOS

I'm trying to run NEON code in my Xcode project for school purposes.
I'm using Xcode 7, LLVM 7.0 and using a device to execute the program.
I have a .s file in the project with the code. If I run just ARM code it works perfectly, but when I add a single NEON line of code (like in the code snippet) I get the following message:
AssemblyTest.s:22:5: error: unrecognized instruction mnemonic
vmul.f32 q14, q8, q8
^
The test code I'm using is
.globl _addInts
.align 2
_addInts:
.cfi_startproc
sub sp, sp, #16
str w0, [sp, #12]
str w1, [sp, #8]
add w0, w0, w1
add sp, sp, #16
vmul.f32 q14, q8, q8 // --- This is the NEON line
ret
.cfi_endproc
Is there any compiler flag I should add to the project? Does LLVM support NEON instructions set?
Thanks!!
As Notlikethat points out you're mixing AArch32 and AArch64.. For example vmul.f32 qx,qy,qz (AArch32) is performing a two float multiply from 64 bit Q registers. While fmul vx.4s,vy.4s,vz.4s (AArch64) is performing 4 float multiplies from 128 bit V registers.
You can mix both architectures in a project by separating them using #ifdefs:
#ifdef __arm__ //AArch32
#ifdef __arm64__ //AArch64
Compile the following using architecture: arm64 in xcode and you're OK. You need to modify the fmul to something useful ;)
sub sp, sp, #16
str w0, [sp, #12]
str w1, [sp, #8]
add w0, w0, w1
add sp, sp, #16
fmul v14.4s, v8.4s, v9.4s
Good luck.
/A

Why is qemu-arm running pthread_join repeatedly in this arm assembly code?

I wrote this arm assembler code calling pthread to implement multi-threading features. I wrote two similar files, but this one is quite tricky.
The main function is:
main:
stmfd sp!, {fp,lr}
add fp, sp, #4
sub sp, sp, #8
sub r3, sp, #8
mov r0, r3
mov r1, #0
ldr r2, .l_thrd1
mov r3, #0
bl pthread_create
ldr r3, [fp, #-8]
mov r0, r3
mov r1, #0
bl pthread_join
...
use objdump to see the related disassembled code:
00405468 <pthread_join>:
405468: e5903068 ldr r3, [r0, #104] ; 0x68
40546c: e92d45f0 push {r4, r5, r6, r7, r8, sl, lr}
405470: e3530000 cmp r3, #0
405474: e24dd014 sub sp, sp, #20
405478: e1a05000 mov r5, r0
40547c: e1a06001 mov r6, r1
405480: ba00004a blt 4055b0 <pthread_join+0x148>
405484: e590321c ldr r3, [r0, #540] ; 0x21c
....
It looks normal, unless it caused the segment error. The qemu.log looks messy and crappy around this:
----------------
IN: pthread_join
INST: isa=[0] opk=[JMP_OP] src={-,-,-,-} dst={-,-} shift={-,-,-} c=[1] s=[-] imm=[24,74] rotate_reg=[-] vfp={-,-,-,-} vfp_val={-,-,-,-} ###
0x00405468: e5903068 ### ldr r3, [r0, #104]
0x0040546c: e92d45f0 ### push {r4, r5, r6, r7, r8, sl, lr}
0x00405470: e3530000 ### cmp r3, #0 ; 0x0
0x00405474: e24dd014 ### sub sp, sp, #20 ; 0x14
0x00405478: e1a05000 ### mov r5, r0
0x0040547c: e1a06001 ### mov r6, r1
0x00405480: ba00004a ### b.lt 0x4055b0
----------------
IN: pthread_join
INST: isa=[0] opk=[JMP_OP] src={-,-,-,-} dst={-,-} shift={-,-,-} c=[1] s=[-] imm=[24,74] rotate_reg=[-] vfp={-,-,-,-} vfp_val={-,-,-,-} ###
0x00405468: e5903068 ### ldr r3, [r0, #104]
0x0040546c: e92d45f0 ### push----------------
IN: start_thread
INST: isa=[0] opk=[JMP_OP] src={-,-,-,-} dst={-,-} shift={-,-,-} c=[0] s=[-] imm=[24,4148] rotate_reg=[-] vfp={-,-,-,-} vfp_val={-,-,-,-} ###
0x00404274: e7802003 ### str{r4, r5 , r6, r7r2, ,r8, sl[r0, , lrr3}]
0x00405470: e3530000 ###
....
Obviously, pthread_join has been entered twice. And at the second time, the 'push' instruction seems not to have been fully executed. The registers also seem normal. I just do not get it.
Another code runs in the right order. They almost same coded.
Nobody's got an answer for the question. I got to answer it myself.
The problem was caused by because stack pointer (r13) was unintentionally saved on the memory and changed by another thread. So r13 pointed to another memory address and caused the segment error.

Resources