Context switch in module's function execute - module

I read that when you run module, the code becomes part of the kernel. But I figured that there can be context switch between processes while module's function is still executing. How could it be? I read that there are no context switches while in kernel.

Certainly there are context switches in the Linux kernel. It's called kernel pre-emption. I'm not sure where you read that there are no context-switches in the kernel or why you'd think it's impossible.
A kernel thread (presumably where your module code will be executing on) is scheduled just like any other user thread.

But the question seems incorrect.
But I figured that there can be context switch between processes while module's function is still executing.
What?
Say we have a module which implements a filesystem. If you open a file on such a filesystem through open syscall, the code goes open -> some lookups -> the code in the module. All of this is happening in the context of the same thread. There is no concept of module context. It may be the code in the module created some kernel threads on its own, but this has zero relation to it being a module.

process could run in one of these context at any given point of time :
user mode
kernel mode
Interrupt mode
Context switch happens in all cases. In Interrupt mode, it is programmer responsibility to disable context switch on the CPU while executing top half section of the interrupt. I dont know where you read that context switching do not happen in kernel mode.

Related

Pyqt5 how to parallel QWidget creations

In my project it is necessary to display a lot of data at once in the mainwindow of my application.
Now I am not quite sure how to implement any parallelization into qt since qt does not allow modification from any other thread than the "main thread" to change ui elements.
Currently I do have only one for loop in my setup function and this creates my QGroupBox object on demand. I already tried to take the work load to other cores, but pickle was not able to process my mainwindow. My other experiment was to start a thread which generates all my QGroupBox for me and send this object via signal back to the "main thread". Also not successful, since addWidget failed because the object was created in another thread with another parent apparently. This confuses me, than the thread has the parent instance of the "main thread" or am I wrong here?

How do I stop a SystemC simulation, from a CTHREAD, and terminate the simulation with a specific exit code?

I've got a SystemC testbench (for a VHDL DUT but that's irrelevant right now). I'd like to be able to cause the test to terminate, with a specific exit code, from within an SC_CTHREAD. I am pretty new to SystemC (I'm mostly a verilog/sv guy).
I've tried simply including "exit(error_code)" but while that terminates the simulation, the final exit code comes from my sc_main's return statement. I guess this makes sense since the "exit" probably terminates the separate thread with that exit code, but not the sc_main thread.
I've tried calling sc_stop before that exit, and usually get an error about calling a pure virtual method (I believe that's end_of_simulation, which I have not defined....but that should be optional, right?).
The only way I see to affect the exit code of the process is to change the return statement of sc_main, but that doesn't quite work when all my action is happening in an SC_THREAD created by a constructor of an object somewhere (if it's not obvious I truly don't understand how SystemC works very well, probably because there's a lot of hidden action with secret classes that we can't see, as opposed to verilog where simulation ordering is all there is, assuming you understand blocking/non-blocking).
Is there a way for the SC_THREAD to pass some failure information up to sc_main?
Note: I am running with a commercial simulation tool, and not pure C++. It's possible that there's an implementation bug there. I'm trying to get our pure C++ compile running but it seems to have broken a while ago (I'm not responsible for the SystemC model).
The answer, of course, is to have sc_main get its return value by reaching directly into the module that it instantiates to get at a member variable:
return(test_module.exit_code);
I need to remember that we're still running C++ and basic C++ rules apply, so this is possible. In my hardware-design mind, I was spinning off modules and threads that had no connection to the classes that they actually are.

Is there any need for GCC's .fini section in a bare-metal microcontroller application?

From what I can find, .fini is used for the destruction of static storage duration objects after main returns. In a typical bare-metal application, main does not return. Is there any reason not to simply remove these symbols from the linker script?
In a typical bare-metal application, main does not return.
main() can indeed return to the startup code, which can be customized to do something after the application exited, e.g. restart it, cut the power, or start a firmware update. It that case, static destructors can be needed.
Is there any reason not to simply remove these symbols from the linker script?
If main() never returns and exit() is never called, then of course you can remove these symols, but then the library startup will miss them, and you'll have to provide a function to override the library function that iterates through __fini_array.
If you are using newlib, you can recompile it with --enable-lite-exit to omit all fini stuff.

How do I execute system calls within a Wind River DKM?

I am trying to make a DKM (Downloadable Kernel Module),"my_dkm.o", that I can load into a custom VxWorks kernel in run-time. I was able to make a simple one (it prints "hello world") but I want my DKM to invoke system calls that already exist within the kernel that is running.
From the shell, I can do -> syscallShow <my_group_#>,1 to give a list of the system calls I want to run. I can also invoke these system calls from the shell, but I don't know how to refer to them when developing my DKM.
Also, the Wind River Workbench help documentation only discusses invoking system calls from RTPs, which doesn't help because I am executing within kernel-space.
Thanks
In Short: You Don't
System call are exclusively to be used by RTPs to make a call to a function that resides in the kernel. The system call itself does a bit of housekeeping and then invokes the underlying kernel routine.
In the context of a DKM, since you are already in the kernel space, you simply need to invoke the same underlying kernel function as the system call.

STM32 programming tips and questions

I could not find any good document on internet about STM32 programming. STM's own documents do not explain anything more than register functions. I will greatly appreciate if anyone can explain my following questions?
I noticed that in all example programs that STM provides, local variables for main() are always defined outside of the main() function (with occasional use of static keyword). Is there any reason for that? Should I follow a similar practice? Should I avoid using local variables inside the main?
I have a gloabal variable which is updated within the clock interrupt handle. I am using the same variable inside another function as a loop condition. Don't I need to access this variable using some form of atomic read operation? How can I know that a clock interrupt does not change its value in the middle of the function execution? Should I need to cancel clock interrupt everytime I need to use this variable inside a function? (However, this seems extremely ineffective to me as I use it as loop condition. I believe there should be better ways of doing it).
Keil automatically inserts a startup code which is written in assembly (i.e. startup_stm32f4xx.s). This startup code has the following import statements:
IMPORT SystemInit
IMPORT __main
.In "C", it makes sense. However, in C++ both main and system_init have different names (e.g. _int_main__void). How can this startup code can still work in C++ even without using "extern "C" " (I tried and it worked). How can the c++ linker (armcc --cpp) can associate these statements with the correct functions?
you can use local or global variables, using local in embedded systems has a risk of your stack colliding with your data. with globals you dont have that problem. but this is true no matter where you are, embedded microcontroller, desktop, etc.
I would make a copy of the global in the foreground task that uses it.
unsigned int myglobal;
void fun ( void )
{
unsigned int myg;
myg=myglobal;
and then only use myg for the rest of the function. Basically you are taking a snapshot and using the snapshot. You would want to do the same thing if you are reading a register, if you want to do multiple things based on a sample of something take one sample of it and make decisions on that one sample, otherwise the item can change between samples. If you are using one global to communicate back and forth to the interrupt handler, well I would use two variables one foreground to interrupt, the other interrupt to foreground. yes, there are times where you need to carefully manage a shared resource like that, normally it has to do with times where you need to do more than one thing, for example if you had several items that all need to change as a group before the handler can see them change then you need to disable the interrupt handler until all the items have changed. here again there is nothing special about embedded microcontrollers this is all basic stuff you would see on a desktop system with a full blown operating system.
Keil knows what they are doing if they support C++ then from a system level they have this worked out. I dont use Keil I use gcc and llvm for microcontrollers like this one.
Edit:
Here is an example of what I am talking about
https://github.com/dwelch67/stm32vld/tree/master/stm32f4d/blinker05
stm32 using timer based interrupts, the interrupt handler modifies a variable shared with the foreground task. The foreground task takes a single snapshot of the shared variable (per loop) and if need be uses the snapshot more than once in the loop rather than the shared variable which can change. This is C not C++ I understand that, and I am using gcc and llvm not Keil. (note llvm has known problems optimizing tight while loops, very old bug, dont know why they have no interest in fixing it, llvm works for this example).
Question 1: Local variables
The sample code provided by ST is not particularly efficient or elegant. It gets the job done, but sometimes there are no good reasons for the things they do.
In general, you use always want your variables to have the smallest scope possible. If you only use a variable in one function, define it inside that function. Add the "static" keyword to local variables if and only if you need them to retain their value after the function is done.
In some embedded environments, like the PIC18 architecture with the C18 compiler, local variables are much more expensive (more program space, slower execution time) than global. On the Cortex M3, that is not true, so you should feel free to use local variables. Check the assembly listing and see for yourself.
Question 2: Sharing variables between interrupts and the main loop
People have written entire chapters explaining the answers to this group of questions. Whenever you share a variable between the main loop and an interrupt, you should definitely use the volatile keywords on it. Variables of 32 or fewer bits can be accessed atomically (unless they are misaligned).
If you need to access a larger variable, or two variables at the same time from the main loop, then you will have to disable the clock interrupt while you are accessing the variables. If your interrupt does not require precise timing, this will not be a problem. When you re-enable the interrupt, it will automatically fire if it needs to.
Question 3: main function in C++
I'm not sure. You can use arm-none-eabi-nm (or whatever nm is called in your toolchain) on your object file to see what symbol name the C++ compiler assigns to main(). I would bet that C++ compilers refrain from mangling the main function for this exact reason, but I'm not sure.
STM's sample code is not an exemplar of good coding practice, it is merely intended to exemplify use of their standard peripheral library (assuming those are the examples you are talking about). In some cases it may be that variables are declared external to main() because they are accessed from an interrupt context (shared memory). There is also perhaps a possibility that it was done that way merely to allow the variables to be watched in the debugger from any context; but that is not a reason to copy the technique. My opinion of STM's example code is that it is generally pretty poor even as example code, let alone from a software engineering point of view.
In this case your clock interrupt variable is atomic so long as it is 32bit or less so long as you are not using read-modify-write semantics with multiple writers. You can safely have one writer, and multiple readers regardless. This is true for this particular platform, but not necessarily universally; the answer may be different for 8 or 16 bit systems, or for multi-core systems for example. The variable should be declared volatile in any case.
I am using C++ on STM32 with Keil, and there is no problem. I am not sure why you think that the C++ entry points are different, they are not here (Keil ARM-MDK v4.22a). The start-up code calls SystemInit() which initialises the PLL and memory timing for example, then calls __main() which performs global static initialisation then calls C++ constructors for global static objects before calling main(). If in doubt, step through the code in the debugger. It is important to note that __main() is not the main() function you write for your application, it is a wrapper with different behaviour for C and C++, but which ultimately calls your main() function.