From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Linux kernel module - memory allocation Date: Mon, 18 May 2015 11:56:22 +0300 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net GJXc8QqWip6cWyHfhODW0gY0d5t2MpGDHPcwgQ9UBWeotKNdS0 Cancel-Lock: sha1:TzMjoq1QDLV25vHFnWe59pV1lDI= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: Xref: news.eternal-september.org comp.lang.ada:25900 Date: 2015-05-18T11:56:22+03:00 List-Id: 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 . @ .