Writing a System Call that Accesses a Kernel Variable - linux-kernel

I have my own kernel module which has a external integer variable:
extern int i = 0;
I want to write a system call (in a separate file) that does the following to i:
i = i + 1;
However, because i is declared in the kernel module file and not in the system call, it won't compile properly. I know the problem doesn't lie elsewhere because the system call "works" fine when all it contains is a printk statement or something similarly self-contained. How can I link the variable in the kernel module file to the system call file?

Related

Forward type not resolved - Lazarus FreePascal

I am trying to use an external library from Pascal that I have successfully used from C before. In order to use this library I have been provided a .h file, a .dll file and a .lib file.
I converted the .h file using the h2pas utility but I am getting the following errors (which I suspect are linker-related):
Error: (5009) Forward type not resolved "XPRSbranchobject"
This appears to be the offending line:
type
...
XPRSbranchobject = ^xo_user_branch_entity_s ;
How do I let Lazarus know that xo_user_branch_entity_s is part of the external library?
You could simply write:
type
xo_user_branch_entity_s = record
a: integer; // <-- probably redundant
end;
XPRSbranchobject = ^xo_user_branch_entity_s;
You must be sure you never (de)allocate such object (the record, either directly or via pointer); if the sources tries to access internal members, the compiler will complain.
This implies that allocation/deallocation is done by the DLL.
It should (could?) work...

Issue with passing of variables from Fortran DLL to main program

I am trying to pass variables (real, integer and double precision etc) from a main program to a DLL and back. I am having some problem when passing variables with data type ‘real’. For example, a variable ‘X’ is defined as real (8) in the DLL project (x=3.0). This variable must go to the main program. ‘X’ is defined as a real(8) in the main program also. But when passing the variable, it is showing a different value for the variable ‘x’ (x= 5.307579804306449D-315) in the main program. I have kept the data type of X same in both the DLL and the main program. I am not able to figure out the issue in passing the variable.

Is it possible to share ruby object with the dll that built with ruby.h?

I have an executable that runs ruby code(RGSS3, precisely) and I also managed to build a DLL that included ruby.h and it's imported to the exe via LoadLibraryA.
I want to ask whether it is possible to share the object/memory between the exe and the dll? If yes, what should I do to make dll access the objects created by the exe and vice versa?
Yes, it is possible.
Ruby objects are represented in C with VALUE, which is basically a pointer. It's a bit hacky to call those functions from RGSS3, though: you have to get the object's memory address (object.object_id << 1) and pass that to the function.
Take a look at this repository, specifically this file. It declares ands exports a function you can use from RPG Maker importing it with Win32API. Supposing you compile the test.c from that repository into test.dll:
# Load RGSS3 dll
LoadLibrary = Win32API.new("kernel32", "LoadLibraryA", "p")
rgss3_dll = LoadLibrary.call("RGSS301.dll")
# Call function from your dll
bitmap = Bitmap.new(32, 32)
BitmapTest = Win32API.new("test.dll", "BitmapTest", "ll")
p BitmapTest.call(rgss3_dll, bitmap.object_id << 1)

dlopen and dylib : main application and dylib address space

My main application statically links to a static library A with a function ABC and my dynamic library xyz.dylib also statically links to the same static library A which has the same function ABC. The function ABC uses a globally defined variable.
Now when the main application Loads xyz.dylib using dlopen on runtime. The initializer gets called where i have called ABC function. This function ABC and uses the global variable from main application address space.
On Osx, functions which are inline the dylib linker will use the first one that is used. So for example, if an inline function is used in your main executable first, and then used in the loaded dylib, it will use the one in the main executable.
This is normally fine, unless your inline makes reference to a global symbol, in which case you are now be using one if your globals for both the dylib, and your executable.
Again this is usually fine, since the same version is used consistently.
The problem happens when you have 2 inline functions that reference a global that is in both executable and dylib, and one function gets used first in the executable, and another one used first in the dylib. Then you have a mismatched pair. For example:
class MagicAlloc
{
void* Alloc() { return gAlloc.get(); }
void Free( void* v ) { gAlloc.free( v ); }
static RealAllocator gAlloc;
};
Suppose you call MagicAlloc::Alloc in the executable, then call it in the dylib, now for all allocations in both you will use the gAlloc in the executable. Then the first call to MagicAlloc::Free happens in the dylib. Then you will try to free something allocated in the binary on the globals from the dylib.
There are two solutions:
Don't use inlines to reference globals/statics. Move the global structure, and the function definitions into the same translation unit ( object file ). Mark the globals "static" so they aren't even visible outside the TLU. Now your functions will be resolved statically in the link step, and bound to the right global.
Hide all the symbols in the executable except the plugin api. Link as normal, but when linking the binary itself pass the following to the linker:
-Wl,-exported_symbols_list,export_file
Where export file is a list of link symbols that should be exported. E.g. you will need to at least have "_main" in that file. Now when your dylib runs it won't be able to dynamically link to the wrong inlines, because they won't be in the dynamic symbol table. The second solution is also more secure, since a malicious plugin won't be able to access globals as easily.

OpenCL, include files

Following the post ,
if I have header file,which has some functions implementations in it and should be included in several kernels(I mean these function are auxilary in all kernels and I don`t want to duplicate the code)
How I make this inclusion - can I keep the functions in header?Will the kernels and the header functions be compiled?
Can you specify (maybe by example) how I use the "-I" option in these case?
I am using VS2010(if its matter at all)
Note:Each kernel runs in different program
Yes, you can use headers in OpenCL for exactly what you are suggesting. Each kernel file will include the header and compile it.
The "-I" option is only used to specify the path for includes. If your includes are in your working directory it is not necessary. Here is an example:
/////////////////////////////////////////////////////////////////
// Load CL file, build CL program object, create CL kernel object
/////////////////////////////////////////////////////////////////
std::string sourceStr = FileToString(params.kernelFile);
cl::Program::Sources sources(1, std::make_pair(sourceStr.c_str(), sourceStr.length()));
cl::Program program = cl::Program(oclHandles.context, sources);
program.build(oclHandles.devices,"-I c:/Includes/");

Resources