How to set up Exception handler for Syscall

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

How to set up Exception handler for Syscall

Postby qheinal » Thu Mar 14, 2024 8:23 pm

How to set up an exception handler for Syscall instruction. I want to use the syscall instruction to access functions etc from Main program. Currently im just using Callx8 to jmp' to the functions fixed Address from external program(App) but even thought it works its not ideal because the Address of function can change as you move code around.

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to set up Exception handler for Syscall

Postby MicroController » Thu Mar 14, 2024 9:13 pm

Can't you let the linker do its thing and manage functions' addresses?

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: How to set up Exception handler for Syscall

Postby qheinal » Fri Mar 15, 2024 11:58 am

its an external app which gets loaded later on and is compiled on its own( Not from arduino ide but directly from assembler). I believe the syscall instruction was made for that purpose. Basically its an app calling functions from main "kernel".

Think of it like how any operating system loads an executable and the executable uses the kernel functions via system calls. Already wrote code for loading apps via sd and running it. Rn the app uses Fixed Address pointers to call functions from "kernel" which as i said is not ideal because everytime you change code in arduino sketch the function's address changes. You have to recompile the user app with function pointer changes which is a hassle.

Anyway im just designing a portable console and to load different games it is necessary. Im not gonna depend on reflashing internal flash chip like how others have done it.

Also if you're wondering why im using xtensa assembler for user app is because it generates position independent code with no dependencies. I might try moving to using xtensa c or c++ compiler later for user app to see if it will work also.

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to set up Exception handler for Syscall

Postby MicroController » Fri Mar 15, 2024 12:34 pm

Ok, I think I get you.
In my understanding, the syscall is indeed intended for calls into an OS. On systems with memory protection /"world controller" its main purpose would be to switch between security contexts.

A common way to achieve the dynamic linking you want is to define a structure that holds a bunch of function pointers. This structure defines the interface between the separate parts of the application, or an 'application' and the runtime environment. When the application is loaded and started, it is passed a pointer to an instance of this structure and from then on uses the provided function pointers to call into the runtime environment.

Of course, this function table can grow quickly because you may have to provide almost everything the dynamic code may need, starting from standard functions like memcpy, math functions, and other stuff from the ROM or cstd, to more complex functions e.g. from the IDF.

I believe gcc already generates position-independent code (PIC) on Xtensas. But you could also look into defining a fixed address to which every 'application' is loaded; this in turn would much resemble the concept of OVERLAYS which may also be possible to leverage with the gcc toolchain.

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to set up Exception handler for Syscall

Postby MicroController » Fri Mar 15, 2024 6:01 pm

Btw: To be able to dynamically load and run code, as an alternative you may want to look into interpreted or bytecode languages.
I find Microvium (JavaScript) intriguing. WASM could have pretty decent performance, and there seem to be a couple of engines which can/could run on an ESP32 (e.g. Espressif's esp-wasmachine, WAMR,...)

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: How to set up Exception handler for Syscall

Postby qheinal » Fri Mar 15, 2024 6:54 pm

I dont need a interpreter. My code runs natively on esp32 ,no translation layer. My question wasnt about Running code its about setting up an exception handler for syscall.

Syscall generates an exception interrupt by design like esp32s illegal instruction or loadstoreerr cpu exceptions. I just want use my own interrupt handler to handle that event. Which is how it was intended to be used to access kernel functions/request from a user app. In arm arduino library for stm32 its a simple thing but i do not know how to set it up for esp32.that is what im asking.

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to set up Exception handler for Syscall

Postby MicroController » Fri Mar 15, 2024 10:10 pm

My question wasnt about Running code...
No, it wasn't. Your question was about how to integrate/dynamically link code with your 'host' application at runtime. Naturally, all interpreted/byte code language engines already have solutions for this problem. As well as memory management for the hosted code, sharing/exchanging data/variables with the runtime, support for code bigger than whatever the host has left of unused IRAM, isolation/protection of the host environment from misbehaving 'guests', &c.

Syscall is not the solution you're looking for. There is xt_set_exception_handler(), but:
Note that some exceptions are handled
by the porting layer itself, and cannot be taken over by application
code in this manner. These are the alloca, syscall, and coprocessor
exceptions.
As I said: You can pass a pointer from the runtime environment to the dynamically loaded code.
Or store a pointer at a defined address just like an interrupt vector table would.

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: How to set up Exception handler for Syscall

Postby qheinal » Sat Mar 16, 2024 6:16 pm

If its in use then oh well but is it possible to use software interrupt. On x86 you can define a software interrupt then just use int [irq number] to trigger it. Idk if thats possible on esp32.

Theres an instruction ILL which always generates exception, i used that but it reboots esp32 after processing the exception. It really should not reboot but idk

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to set up Exception handler for Syscall

Postby MicroController » Sat Mar 16, 2024 9:13 pm

Yeah, apparently software interrupts aren't really a thing on the Xtensa architecture.
On the ESPs, there should be ways to trigger a CPU (internal) interrupt by setting a bit in some interrupt status register, but details are scarce.

qheinal
Posts: 25
Joined: Tue Feb 20, 2024 4:38 pm

Re: How to set up Exception handler for Syscall

Postby qheinal » Sun Mar 17, 2024 1:53 am

Yeh its really hard to find stuff for xtensa architecture. btw I solved the issue of restarting on illegal exception by changing the pc register. So syscalls via the ILL instruction works now. Right now it checks the value of certain registers to determine if its done by the ILL instruction intentionally or unintentionally

Thanks for the help

Who is online

Users browsing this forum: No registered users and 89 guests