Reputation: 518
TLDR: Is it possible to map an adresspace to a function call in a kernel module?
So similar to mmap. But mmap is for userspace and will only call the functions if you access the next page.
+---+---+---+---+--------------------+
| | | | | |
+-------+---+---+--------------------+
|
+-------------------------------------------------+
|
+---------------------v----------------+
| void my_driver_function(int offset); |
+--------------------------------------+
EDIT:
In our old world we had N Devices that where controlled by N independent device drivers. Each registers of each device where memory mapped at a location. And every driver just ioremaped the registers and controlled the hardware directly.
+----------------+ +--------------+ +------------+ +--------------+
| Driver A | | Driver B | | Driver C | | Driver E |
+----------------+ +--------------+ +------------+ +--------------+
|ioremap |ioremap |ioremap |ioremap
| | | |
+----+-----------+ +--------------+ +------------+ +--------------+
| Device A | | Device B | | Device C | | Device E |
| | | | | | | |
+----------------+ +--------------+ +------------+ +--------------+
In our new world we merged the device hardware. But the drivers are still separate. Due to hardware limitations, it is now necessary to mutex all access to the one new device. Also there are now some more constraints that where not there in the old world (alignment, byteorder, timing, ...). But because the drivers are independent they do not know about code or access happening in another driver. So it leads to violations of this constraints.
+----------------+ +--------------+ +------------+ +--------------+
| Driver A | | Driver B | | Driver C | | Driver E |
+----------------+ +--------------+ +------------+ +--------------+
|ioremap |ioremap |ioremap |ioremap
| | | |
+----+----------------------------------------+-----------------+---------+
| Device A/B/C/D/E |
| |
+-------------------------------------------------------------------------+
because we do not want to rework all the drivers. go through all the code and seek for every pointer that my be pointing to a hardware register to guard all this accesses by mutexes, my idea was to add a virtual device memory. This should be a memory area there each access will be routed to a function. This function will then perform locking and tracking and things and access to the hardware.
+----------------+ +--------------+ +------------+ +--------------+
| Driver A | | Driver B | | Driver C | | Driver E |
+----------------+ +--------------+ +------------+ +--------------+
|ioremap |ioremap |ioremap |ioremap
| | | |
+----+----------------------------------------+-----------------+---------+
| Virtual Device A/B/C/D/E |
| |
+----------------------+--------------------------------------------------+
|
|my_mapper_function(...)
| /* do (un)locking, check constraints, ... */
|
+----------------------+--------------------------------------------------+
| Device A/B/C/D/E |
| |
+-------------------------------------------------------------------------+
is there a mechanism in the linux kernel, that allows to map every access to a specific memory region to be routed trough a function? Similar to what mmap does, but actually quite different because you cannot hook every arbitrary function to mmap. Also it will not route every request through this function but only if a request crosses a page border.
Upvotes: 0
Views: 212
Reputation: 573
I am thinking of moving Driver A/B/C... to UserSpace using UIO, and all these driver using same kernel space code which used to control all related devices.
User Space | Driver A | Driver B | Driver C |
|================================|
Kernel Space | KObject ABC |
|================================|
Hardware | Device A|B|C |
Upvotes: 1