Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running FreeRTOS on ARM Cortex-M4 CPU #1893

Open
pipe01 opened this issue Oct 20, 2023 · 1 comment
Open

Running FreeRTOS on ARM Cortex-M4 CPU #1893

pipe01 opened this issue Oct 20, 2023 · 1 comment

Comments

@pipe01
Copy link

pipe01 commented Oct 20, 2023

I'm trying to run an app built using FreeRTOS using Unicorn, but I can't seem to get the exception handlers right.

Relevant FreeRTOS code:

0000413c <vPortStartFirstTask>:
    413c:	4805      	ldr	r0, [pc, #20]	; (4154 <vPortStartFirstTask+0x18>)
    413e:	6800      	ldr	r0, [r0, #0]
    4140:	f380 8808 	msr	MSP, r0
    4144:	b662      	cpsie	i
    4146:	b661      	cpsie	f
    4148:	f3bf 8f4f 	dsb	sy
    414c:	f3bf 8f6f 	isb	sy
    4150:	df00      	svc	0
    4152:	bf00      	nop
    4154:	00000000 	andeq	r0, r0, r0

00004158 <SVC_Handler>:
    4158:	4b06      	ldr	r3, [pc, #24]	; (4174 <SVC_Handler+0x1c>)
    415a:	6819      	ldr	r1, [r3, #0]
    415c:	6808      	ldr	r0, [r1, #0]
    415e:	e8b0 4ff0 	ldmia.w	r0!, {r4, r5, r6, r7, r8, r9, sl, fp, lr}
    4162:	f380 8809 	msr	PSP, r0
    4166:	f3bf 8f6f 	isb	sy
    416a:	f04f 0000 	mov.w	r0, #0
    416e:	f380 8811 	msr	BASEPRI, r0
    4172:	4770      	bx	lr
    4174:	2000c6c8 	andcs	ip, r0, r8, asr #13

When the svc 0 exception is raised I set the PC to the corresponding entry in the vector table, which is hard-coded at the moment:

p, _ := mu.MemRead(11*4, 4)
mu.RegWrite(uc.ARM_REG_PC, uint64(binary.LittleEndian.Uint32(p)))

This works until execution gets to 0x4172, where the lr register is pointing to garbage (0x0xfffffffd) so when the code jumps there it dies. The lr register points to a sensible address until the msr PSP, r0 call, which is when it changes into garbage. Any idea what I'm doing wrong?

@lockbox
Copy link

lockbox commented Oct 30, 2023

See page B1-539 here. That is proper return address from an exception, you need to hook it and handle it how your use case requires.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants