comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Linux kernel module - memory allocation
Date: Mon, 18 May 2015 11:56:22 +0300
Date: 2015-05-18T11:56:22+03:00	[thread overview]
Message-ID: <crtnpmFj143U1@mid.individual.net> (raw)
In-Reply-To: <slrnmlj8k2.9gf.achsis_of_elvis@tmoeller-laptop.ad.here.com>

On 15-05-18 11:27 , q.kontinuum wrote:
> I'm new to this group, new to Ada programming, and a bit out of touch with
> Linux driver development for some time. Since I want to learn Ada and want
> to get back in touch with Linux I'm trying to implement a driver for my
> raspberry-pi, to access a DS18B20 temperature sensor via onewire protocol.

I don't know much about the Linux driver environment, but perhaps I can 
help you a bit...

Do you mean a real "driver", either kernel-linked or a loadable module? 
Or do you mean user-land SW to operate the sensor through some existing 
I/O driver for the I/O channel? From the error messages you get, I guess 
you want to make a loadable driver module.

Which Ada compiler are you using? Do you use some specific compiler 
switches to adapt it to the driver environment?

> The steps I already took was to implement a small wrapper in C to link
> with a first Hello world module in Ada and to access the hr_timer of the
> Linux kernel (here I again used to wrapper and called it from Ada to avoid
> cloning the whole hrtimer struct from Linux kernel header files)

Yes, it is often simpler to have a C wrapper, if the header files are 
complex. But GNAT can also translate some C headers into Ada 
declarations automatically.

> Where I'm getting stuck now is a function returning a String. I simplified
> the function to this:
>
>     function UInt2String(I : Integer) return String is
>     begin
>        return "";
>     end;
>
> As soon as this function is used somewhere, I can't load the module.
> Here is the kernel log output when I try to load the module:
>
> May 18 10:19:39 localhost kernel: [87978.938801] wrapper:
>     Unknown symbol system__secondary_stack__ss_allocate (err 0)

A function that returns a value of run-time dynamic size, such as the 
unconstrained String in your example, needs support from the Ada 
run-time system (RTS). In the GNAT compiler, this support takes the form 
of a "secondary stack" that is used to hold such data of dynamic size, 
but with first-in-first-out life-times.

The error message you get shows that the kernel (of course) does not 
provide this Ada-specific support.

You could try with a constrained string subtype, say String(1 .. 10), 
but I suspect that you should be using some very specific GNAT switches 
to completely eliminate any dependency on the GNAT RTS (which will of 
course constrain the kind of Ada code you can run in this environment).

> I will need to store a binary tree later on to store the addresses of the
> devices found on the onewire bus. In C, I'd probably use kmalloc and
> casts to allocate memory for a node of the tree. How should I do that in
> Ada?

If kmalloc is what your environment (kernel) provides, I suppose that is 
what you should use. Perhaps you can wrap it up as a user-defined Ada 
"storage pool" (see the Ada RM 13.11) which would let you use the "new" 
statement, or you can write an Ada binding in the same way as for your C 
wrappers. kmalloc seems to return a C "void *". Assuming that this is 
implemented as an address, you should be able to use the predefined Ada 
package System.Address_To_Access_Conversions to convert the kmalloc 
result to a pointer of any type you want (but beware memory alignment 
errors -- the documentation for kmalloc that I found did not promise to 
return memory aligned in any particular way).

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

  reply	other threads:[~2015-05-18  8:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-18  8:27 Linux kernel module - memory allocation q.kontinuum
2015-05-18  8:56 ` Niklas Holsti [this message]
2015-05-18  9:19   ` q.kontinuum
2015-05-18 16:02     ` Niklas Holsti
2015-05-18 17:34       ` Simon Clubley
2015-05-18 19:25         ` jan.de.kruyf
2015-05-19  5:30           ` q.kontinuum
2015-05-19  7:18             ` jan.de.kruyf
2015-05-19  7:56               ` q.kontinuum
2015-05-19  8:06                 ` jan.de.kruyf
2015-05-19 10:20                   ` Mart van de Wege
2015-05-19 11:14                     ` jan.de.kruyf
2015-05-19 11:16                   ` G.B.
2015-05-19  9:52               ` Simon Wright
2015-05-19 11:11                 ` jan.de.kruyf
2015-05-18 19:48         ` q.kontinuum
2015-05-18 20:18           ` rrr.eee.27
2015-05-18 21:09             ` q.kontinuum
2015-05-18 19:35       ` q.kontinuum
2015-05-18 20:19 ` jan.de.kruyf
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox