I am familiar with the instruction int on x86.
Is it possible to inline assembly int my_unique_number and use
requst_irq(my_unique_number , function); with a function to be
called when the instruction is executed?
What about ARM is there any similar way to use SWI immed_8 instruction or SVC?
Can I do the same trick?
In the Linux kernel it's much simpler to simply implement your own system call in the kernel and use the syscall(your_number) function to call it rather than doing the assembly code yourself.
Doing so is very easy http://www.linuxjournal.com/article/3326 here's a link to an article that describes how to do so in much less words than I would have used.
Related
I want to access the register file of the RISC-V using the C Program.
is there any way I can write and read directly into the register file of the RISC-V using C Program?
There is no portable way to access registers using the C language, as C works at a level of abstraction above registers / RTL (roughly speaking).
However there may be compiler-specific ways to do this, using built-in compiler intrinsic functions. See:
https://gcc.gnu.org/onlinedocs/gcc/Target-Builtins.html
https://gcc.gnu.org/onlinedocs/gcc/Explicit-Register-Variables.html
https://gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html
https://gcc.gnu.org/onlinedocs/gccint/Registers.html
See these other StackOverflow questions:
Accessing a register without using inline assembly with gcc
Reading a register value into a C variable
EDIT
RISC-V register access doesn't seem supported with C-intrinsics using GCC. You may have to use (inline) assembly to get at it.
EDIT2
Some (most?) embedded processors running bare-metal without OS (i.e. NOT in a virtual-memory system) can access their registers if you know their address. But it depends much on the hardware you're running on. As Erik Eidt asks: What are you trying to do?
Like the CR3 register which is used to point to the page directory. Linux also uses paging and is written in C, but how are these registers used in C (how to select a particular register using C)?
The C language provides no way to access specific processor registers. This is all up to the compiler.
To access specific registers you would have to write at least this part of your code in assembler.
The registers you are talking about are not a property of the language but the property of the hardware on which you run your programs. I believe that you are talking about an x86 type hardware. cr0-4 and orther specific regs are a property of the operating system and are managed by it, including paging table.
So, the language does not provide a way to access those hw-specific registers. The only way is to write an assembly code (hardware-specific) to manipulate them. The only thing which the language provides is the asm() operator which allows to insert assembly code in the program.
Standard C does not provide any facility to directly access processor registers. Some implementations may provide extensions that allow you to embed assembly code in your C code (such as the asm extension provided by gcc).
Generally speaking, if you need direct access to a processor register (or other hardware-specific location), you'd write that routine in assembler and link it into the larger program.
I want to write a c program that can detect CPU id for activation process.
Some advise me to use UUID but I like to try CPU id instead. After searching I found some good answer. But I am using Windows 8 64bit and Mingw. So I am afraid I cant use it.
Yes, two pieces of information, since you wanted to be guided and not shown code :)
Use the CPUID x86 instruction http://x86.renejeschke.de/html/file_module_x86_id_45.html
... with gnu inline assembler syntax for C programs
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
That's it. Create a small inline C function that has an asm {} block in it and the single instruction inthere would be CPUID.Be aware of how to return values from these assembly blocks, gcc has a special syntax for it.
With this info, I reckon you can solve your problem.
I'm teaching myself Linux assembly language and I've come across an interesting difference between BSD and Linux. In Unix, you push your syscall parameters onto the stack before calling an 80h interrupt; by contrast, in Linux, you pass your parameters in registers.
Does anyone know what the rationale was for the Linux developers to go with registers instead of the stack?
Note: Here's a good page detailing this difference: FreeBSD Developer's Handbook:System Calls without explaining the rationale.
The syscall convention is different because the standard function calling sequence is different. Im assuming you're talking about the difference between the x86-32 calling convention and the AMD64 calling convention. You can check out the AMD64 ABI here.
But if you want to get to the point quickly check this post. Basically it's about speed. By changing the calling convention and using registers instead of the stack you can shave off instructions in the prologue and the epilogue of a call.
You can use some registers with 32 bit code as well. There are several calling conventions for 32-bit code: cdecl, stdcall, pascal and fastcall. Windows and Linux use the same calling conventions for 32-bit code. With fastcall (__attribute((fastcall) in GCC) the first two integer parameters (3 with some compilers) can be registers. The other calling conventions use the stack.
For 64-bit code Windows and Linux use different calling conventions. Linux can use up to 14 registers for calls and Windows only six. Using registers can make the code faster. That could be part of the reason some 64-bit code with many function calls runs O(10%) faster than the same 32-bit code.
Is it possible to programmatically break into debugger from GCC?
For example I want something like:
#define STOP_EXECUTION_HERE ???
which when put on some code line will force debugger stop there.
Is it possible at all ?
I found some solution, but i can't use it because on my embedded ARM system I don't have signal.h.
(However I can use inline assembly).
What you are trying to do is called software breakpoint
It is very hard to say precisely without knowing how you actually debug. I assume your embedded system runs gdbstub. There are various possibilities how this can be supported:
Use dedicated BKPT instruction
This could be a standard way on your system and debugger to support software breakpoints
Feed invalid instruction to CPU
gdbstub could have placed own UNDEF ARM mode handler placed. If you go this route you must be aware of current CPU mode (ARM or THUMB), because instruction size will be different. Examples of undefined instructions:
ARM: 0xE7F123F4
THUMB: 0xDE56
In runtime CPU mode could be found from the lowest bit of PC register. But the easier way is to know how you compiled object file, where you placed software breakpoint
Use SWI instruction
We did so when used RealView ICE. Most likely this does not apply to you, if you run some OS on your embedded system. SWI is usually used by OS to implement system calls
Normally, the best way to do this is via a library function supplied with your device's toolchain.
I can't test it out, but a general solution might be to insert an ARM BKPT instruction. It takes an immediate argument, which your debugger might interpret, with potentially odd results.
You could run your application from GDB, and in the code call e.g. abort. This will stop your application at that point. I'm not sure if it's possible to continue after that though.