comp.lang.ada
 help / color / mirror / Atom feed
* Kernel Syscall from Ada?
@ 2016-06-23  8:36 Diogenes
  2016-06-23 10:58 ` Björn Lundin
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Diogenes @ 2016-06-23  8:36 UTC (permalink / raw)


Is there a simple way to make a direct (Linux)Kernel syscall from Ada without using the system C library? i.e. Make a direct call as in Assembler?

I'm asking because I've found a way to strip about 80k from a statically linked executable by not including Interfaces.C in the runtime.

I've gotten my code to work reasonably well (no segfaults or memory errors) using inline Assembler. But it seems like there should be an Abstract or Generic "syscall" feature as part of the System library that we could use for doing our own Kernel calls. Same thing for the vDSO.

Any tips?

Diogenes


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
@ 2016-06-23 10:58 ` Björn Lundin
  2016-06-23 16:28   ` Per Sandberg
  2016-06-23 22:18 ` Randy Brukardt
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Björn Lundin @ 2016-06-23 10:58 UTC (permalink / raw)


On 2016-06-23 10:36, Diogenes wrote:
> Is there a simple way to make a direct (Linux)Kernel syscall from Ada without using the system C library? i.e. Make a direct call as in Assembler?
> 
> I'm asking because I've found a way to strip about 80k from a statically linked executable by not including Interfaces.C in the runtime.
> 
> I've gotten my code to work reasonably well (no segfaults or memory errors) using inline Assembler. But it seems like there should be an Abstract or Generic "syscall" feature as part of the System library that we could use for doing our own Kernel calls. Same thing for the vDSO.
> 
> Any tips?
> 
> Diogenes
> 


pragma import ?


-- 
--
Björn


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23 10:58 ` Björn Lundin
@ 2016-06-23 16:28   ` Per Sandberg
  0 siblings, 0 replies; 13+ messages in thread
From: Per Sandberg @ 2016-06-23 16:28 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 823 bytes --]

The attached makefile may provide some initial directions in addition to 
"pragma import".

/Per

Den 2016-06-23 kl. 12:58, skrev Björn Lundin:
> On 2016-06-23 10:36, Diogenes wrote:
>> Is there a simple way to make a direct (Linux)Kernel syscall from Ada without using the system C library? i.e. Make a direct call as in Assembler?
>>
>> I'm asking because I've found a way to strip about 80k from a statically linked executable by not including Interfaces.C in the runtime.
>>
>> I've gotten my code to work reasonably well (no segfaults or memory errors) using inline Assembler. But it seems like there should be an Abstract or Generic "syscall" feature as part of the System library that we could use for doing our own Kernel calls. Same thing for the vDSO.
>>
>> Any tips?
>>
>> Diogenes
>>
>
>
> pragma import ?
>
>

[-- Attachment #2: Makefile --]
[-- Type: text/plain, Size: 220 bytes --]

all:
	rm -f *.ads *.ali *.o *.d 
	(cd /usr/include ; find sys -type f) | grep -v -e vm86.h -e elf.h | sed -e "s-^-#include <-"  -e "s-\$$->-" >gen.c
	gcc -c gen.c -fdump-ada-spec -fada-spec-parent=kernel
	rm -f *.c *.o


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
  2016-06-23 10:58 ` Björn Lundin
@ 2016-06-23 22:18 ` Randy Brukardt
  2018-07-12 19:07   ` Dan'l Miller
  2016-06-24  1:13 ` Xavier Petit
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Randy Brukardt @ 2016-06-23 22:18 UTC (permalink / raw)


"Diogenes" <phathax0r@gmail.com> wrote in message 
news:2048d6d6-04e2-4e2c-9483-e3769da59781@googlegroups.com...
>Is there a simple way to make a direct (Linux)Kernel syscall from Ada 
>without
>using the system C library? i.e. Make a direct call as in Assembler?

On almost all modern systems, a system call IS a call to some C subprogram. 
(On Windows, they're calls into a DLL; Linux appears similar although I've 
never studied the details.) There's nothing like the set-registers and trap 
like there was in the old days on MS-DOS, XENIX, and other systems.

So the only way to make a system call is via aspect Import to the needed C 
routine. (On Windows, I''d expect that a program that didn't do any system 
calls would be much smaller than a "normal" program; Windows needs some 
stuff added to every program; it doesn't have anything to do with Ada 
per-se.)

                                  Randy.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
  2016-06-23 10:58 ` Björn Lundin
  2016-06-23 22:18 ` Randy Brukardt
@ 2016-06-24  1:13 ` Xavier Petit
  2016-06-24 22:23 ` Florian Weimer
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Xavier Petit @ 2016-06-24  1:13 UTC (permalink / raw)


On windows :

procedure Beethoven is
    type Dword is mod 2 ** 32 with Size => 32;

    procedure Beep (Frequency, Period : in Dword)
      with Import, Convention => Stdcall, External_Name => "Beep";
begin
    Beep (784, 250);
    Beep (784, 250);
    Beep (784, 250);
    Beep (622, 1000);
end;

Maybe you are looking at this "Stdcall" convention ?
Also regarding small executables, I LOVE this one :
http://madebyevan.com/diskitude/

(false positive for a few antivirus)

10kb (static) for a very useful windows tool (use right click to 
zoom-in/out to folders), very impressive, too bad the author doesn't 
give the source code...

-- 
Xavier Petit

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
                   ` (2 preceding siblings ...)
  2016-06-24  1:13 ` Xavier Petit
@ 2016-06-24 22:23 ` Florian Weimer
  2018-07-11 22:38 ` alexgrantbenedict
  2018-07-12  1:32 ` Dan'l Miller
  5 siblings, 0 replies; 13+ messages in thread
From: Florian Weimer @ 2016-06-24 22:23 UTC (permalink / raw)


* Diogenes:

> Is there a simple way to make a direct (Linux)Kernel syscall from Ada
> without using the system C library? i.e. Make a direct call as in
> Assembler?

glibc implements a lot of system calls as assembler stubs.  Some are
more complicated because emulation of certain features is desirable,
or there is no straight mapping between the application and kernel
parameter lists (e.g., the offset parameter to mmap).

Calling individual system calls in GNAT using machine code insertion
is not that hard (except for 6-argument system calls on i386 with
older GCC versions).  But designing a way to write such system calls
so that you do not have to tweak the sources for each architecture is
quite hard.  The lax C type system allows you to treat all system
calls as taking signed long arguments, returning signed long (*), and
you only have to specialize the number of arguments.  With the C
preprocessor, you can also deal with 64-bit arguments in a portable
manner (so that the source code denoting the system call is identical
across 32-bit and 64-bit architectures).  I doubt something similar is
possible in GNAT.

You also need to replicate all data types used in the userspace API,
which can be rather involved (stat is a bit complicated, and there are
many ioctls).

(*) Some architectures do not use in-band signaling for errno, and the
return convention is different.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
                   ` (3 preceding siblings ...)
  2016-06-24 22:23 ` Florian Weimer
@ 2018-07-11 22:38 ` alexgrantbenedict
  2018-07-12  1:32 ` Dan'l Miller
  5 siblings, 0 replies; 13+ messages in thread
From: alexgrantbenedict @ 2018-07-11 22:38 UTC (permalink / raw)


On Thursday, June 23, 2016 at 2:36:21 AM UTC-6, Diogenes wrote:
> Is there a simple way to make a direct (Linux)Kernel syscall from Ada without using the system C library? i.e. Make a direct call as in Assembler?
> 
> I'm asking because I've found a way to strip about 80k from a statically linked executable by not including Interfaces.C in the runtime.
> 
> I've gotten my code to work reasonably well (no segfaults or memory errors) using inline Assembler. But it seems like there should be an Abstract or Generic "syscall" feature as part of the System library that we could use for doing our own Kernel calls. Same thing for the vDSO.
> 
> Any tips?
> 
> Diogenes


I did something similar for a work thing a while back, I posted it to github https://github.com/abenedic/ada-raw-linux-syscalls . It still needs to be cleaned up though.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
                   ` (4 preceding siblings ...)
  2018-07-11 22:38 ` alexgrantbenedict
@ 2018-07-12  1:32 ` Dan'l Miller
  2018-07-12  8:19   ` Lucretia
  5 siblings, 1 reply; 13+ messages in thread
From: Dan'l Miller @ 2018-07-12  1:32 UTC (permalink / raw)


On Thursday, June 23, 2016 at 3:36:21 AM UTC-5, Diogenes wrote:
> Is there a simple way to make a direct (Linux)Kernel syscall from Ada without using the system C library? i.e. Make a direct call as in Assembler?
> 
> I'm asking because I've found a way to strip about 80k from a statically linked executable by not including Interfaces.C in the runtime.
> 
> I've gotten my code to work reasonably well (no segfaults or memory errors) using inline Assembler. But it seems like there should be an Abstract or Generic "syscall" feature as part of the System library that we could use for doing our own Kernel calls. Same thing for the vDSO.
> 
> Any tips?
> 
> Diogenes

Well, coming at the topic top down from the momentum of ISO/IEEE standardization, for system calls in POSIX.2 (C API) there does exist an analogous POSIX.5 (Ada API).  Whenever possible this should be utilized (and extended/maintained where stale).
front-matter preview:
https://webstore.iec.ch/preview/info_isoiec14519%7Bed2.0%7Den.pdf
available for purchase in each major national standards body, such as for $232 in the USA:
https://webstore.ansi.org/RecordDetail.aspx?sku=ISO%2FIEC%2014519:2001&source=preview

Coming at the topic bottom up from Linux kernel space, the C-language system calls could be augmented with a wholesale rethink of all the system calls into Ada-speak as a new Ada-centric distribution of Linux.
https://www.kernel.org/doc/html/v4.10/process/adding-syscalls.html


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2018-07-12  1:32 ` Dan'l Miller
@ 2018-07-12  8:19   ` Lucretia
  2018-07-12 15:27     ` Dan'l Miller
  0 siblings, 1 reply; 13+ messages in thread
From: Lucretia @ 2018-07-12  8:19 UTC (permalink / raw)


On Thursday, 12 July 2018 02:32:30 UTC+1, Dan'l Miller  wrote:

> available for purchase in each major national standards body, such as for $232 in the USA:
> https://webstore.ansi.org/RecordDetail.aspx?sku=ISO%2FIEC%2014519:2001&source=preview

Bit pricey, really, for a book. I can see this interface being rewritten cleanroom tbh in Ada 2012, i.e. the api won't be the same.
 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2018-07-12  8:19   ` Lucretia
@ 2018-07-12 15:27     ` Dan'l Miller
  2018-07-12 17:27       ` Lucretia
  0 siblings, 1 reply; 13+ messages in thread
From: Dan'l Miller @ 2018-07-12 15:27 UTC (permalink / raw)


On Thursday, July 12, 2018 at 3:19:34 AM UTC-5, Lucretia wrote:
> On Thursday, 12 July 2018 02:32:30 UTC+1, Dan'l Miller  wrote:
> 
> > available for purchase in each major national standards body, such as for $232 in the USA:
> > https://webstore.ansi.org/RecordDetail.aspx?sku=ISO%2FIEC%2014519:2001&source=preview
> 
> Bit pricey, really, for a book. I can see this interface being rewritten cleanroom tbh in Ada 2012, i.e. the
> api won't be the same.

https://en.wikibooks.org/wiki/Ada_Programming/Platform/POSIX

Yes, the POSIX.5 standard and Florist implementation thereof is Ada95 era.  As a thick binding, it likely could be freshened up a tad to Ada2012.  If the latest public version from AdaCore is strictly GPLed, then the older editions from Florida State should be GMGPL instead.

Acquiring a copy of the standard instead (e.g., borrowing it from an engineering library at a university) would permit a fairly easy cleanroom reimplementation directly from the standard.  That re-implementation could provide Ada95, Ada2005, and Ada2012 personalities, perhaps going far beyond the standard for Ada2012.  But ultimately, the result is still an add-on layer to the C-language syscall …

… I still think what would put Ada on the map via public fanfare would be an Ada-centric distribution of Linux (or BSD Unix for that matter, or both) that add a whole set of Ada-only syscalls in a new Ada-only syscall table to put Ada on 100% equal footing as C for Linux programming.  Not even C++ has that.  (What better way to compete with C++?)  Ada should walk confidently in through the front door.  In all the POSIX.2 (C API) areas of Linux syscalls, the Ada syscalls perhaps should be the POSIX.5 (Ada thick binding) verbatim (at least when in Ada95 mode).

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2018-07-12 15:27     ` Dan'l Miller
@ 2018-07-12 17:27       ` Lucretia
  0 siblings, 0 replies; 13+ messages in thread
From: Lucretia @ 2018-07-12 17:27 UTC (permalink / raw)


On Thursday, 12 July 2018 16:27:39 UTC+1, Dan'l Miller  wrote:

> Acquiring a copy of the standard instead (e.g., borrowing it from an engineering library at a university) would permit a fairly easy cleanroom reimplementation directly from the standard.  That re-implementation could provide Ada95, Ada2005, and Ada2012 personalities, perhaps going far beyond the standard for Ada2012.  But ultimately, the result is still an add-on layer to the C-language syscall …

If you do a search for that standard, you'll find some pdf's where they've talked about removing bindings because Ada has the same functionality in the library; don't they realise that you need those interfaces to create those api's in the Ada library??

> … I still think what would put Ada on the map via public fanfare would be an Ada-centric distribution of Linux (or BSD Unix for that matter, or both) that add a whole set of Ada-only syscalls in a new Ada-only syscall table to put Ada on 100% equal footing as C for Linux programming.  Not even C++ has that.  (What better way to compete with C++?)  Ada should walk confidently in through the front door.  In all the POSIX.2 (C API) areas of Linux syscalls, the Ada syscalls perhaps should be the POSIX.5 (Ada thick binding) verbatim (at least when in Ada95 mode).

Possible, I've thought a bit about that today.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2016-06-23 22:18 ` Randy Brukardt
@ 2018-07-12 19:07   ` Dan'l Miller
  2018-07-12 20:59     ` Randy Brukardt
  0 siblings, 1 reply; 13+ messages in thread
From: Dan'l Miller @ 2018-07-12 19:07 UTC (permalink / raw)


On Thursday, June 23, 2016 at 5:18:14 PM UTC-5, Randy Brukardt wrote:
> "Diogenes"  wrote in message 
> news:2048d6d6-04e2-4e2c-9483-e3769da59781@googlegroups.com...
> >Is there a simple way to make a direct (Linux)Kernel syscall from Ada 
> >without
> >using the system C library? i.e. Make a direct call as in Assembler?
> 
> On almost all modern systems, a system call IS a call to some C subprogram. 
> (On Windows, they're calls into a DLL; Linux appears similar although I've 
> never studied the details.) There's nothing like the set-registers and trap 
> like there was in the old days on MS-DOS, XENIX, and other systems.
> 
> So the only way to make a system call is via aspect Import to the needed C 
> routine. (On Windows, I''d expect that a program that didn't do any system 
> calls would be much smaller than a "normal" program; Windows needs some 
> stuff added to every program; it doesn't have anything to do with Ada 
> per-se.)
> 
>                                   Randy.

One of the more succinct summaries of the legacy 8086, 32-bit x86, and 64-bit x86-64:
https://stackoverflow.com/questions/12806584/what-is-better-int-0x80-or-syscall

Randy is mostly accurate:  interrupt 80₁₆ has faded into antiquity and currently everything about system calls boils down to C subprograms.

Conversely, the syscall mechanism is best thought of as an •assembly• subprogram (and calling convention) that crosses a ring boundary whose syscall-table array of assembly subprograms does have some vague resemblance to the old interrupt 80₁₆ vector table.  (No processor manufacturer would hardcode C-language conventions into their silicon for context switches between protection rings.  It is all C out of habit, not hardware-imposed necessity.)

Conceivably Ada could present its own array of Ada subprograms in its own syscall-table, so that Ada would not come down to invoking C subprograms in userspace.  An Ada-centric new distribution of Linux (or BSD Unix or GNU Hurd or any other operating system, for that matter) would be needed to add an Ada-only syscall vector/lookup table (LUT) in addition to the current C-only syscall LUT.  But such a new distro of Ada-Linux (or Ada-BSD or Ada-Hurd) could showcase 100%-Ada software in userspace, encourage Ada programming in kernelspace, and use that Ada-based package manager announced recently on c.l.a.

lucid explanation of 32-bit sysenter:
http://articles.manugarg.com/systemcallinlinux2_6.html

a taste of VDSO to avoid a userspace-to-kernelspace context switch for some would-be/has-been syscalls:
https://en.wikipedia.org/wiki/VDSO

the complexity of on which processors AMD syscall versus Intel sysenter instructions are supported or not: 
https://ReverseEngineering.stackexchange.com/questions/16454/struggling-between-syscall-or-sysenter-windows


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Kernel Syscall from Ada?
  2018-07-12 19:07   ` Dan'l Miller
@ 2018-07-12 20:59     ` Randy Brukardt
  0 siblings, 0 replies; 13+ messages in thread
From: Randy Brukardt @ 2018-07-12 20:59 UTC (permalink / raw)


"Dan'l Miller" <optikos@verizon.net> wrote in message 
news:e9c05e73-ea72-4fc5-a671-fecd3fdf0c11@googlegroups.com...
...
>One of the more succinct summaries of the legacy 8086, 32-bit x86, and 
>64-bit x86-64:
>https://stackoverflow.com/questions/12806584/what-is-better-int-0x80-or-syscall
>
>Randy is mostly accurate:  interrupt 80?? has faded into antiquity and 
>currently
>everything about system calls boils down to C subprograms.

Interesting; I've never seen anything about Linux system traps in the past. 
That would make it easier to port Janus/Ada to Linux without having to 
involve GCC (one would want an *option* to link with GCC, but ideally no 
*requirement* to do that). The old SCO Unix compiler directly used system 
traps to talk to the kernel; no C libraries were required. That meant it was 
easy to create Ada programs just using Ada tools and there wasn't any 
dependencies on anything else. That's not really practical for Windows, so 
I'm surprised to find out that Linux is more like the old MS-DOS and Unix 
systems in this regard.

                                          Randy.



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-07-12 20:59 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-23  8:36 Kernel Syscall from Ada? Diogenes
2016-06-23 10:58 ` Björn Lundin
2016-06-23 16:28   ` Per Sandberg
2016-06-23 22:18 ` Randy Brukardt
2018-07-12 19:07   ` Dan'l Miller
2018-07-12 20:59     ` Randy Brukardt
2016-06-24  1:13 ` Xavier Petit
2016-06-24 22:23 ` Florian Weimer
2018-07-11 22:38 ` alexgrantbenedict
2018-07-12  1:32 ` Dan'l Miller
2018-07-12  8:19   ` Lucretia
2018-07-12 15:27     ` Dan'l Miller
2018-07-12 17:27       ` Lucretia

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