For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > Shared memory internals?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Shared memory internals?
Chinlu

2006-09-26, 7:00 pm

Hi there,

I've been looking at how shared memory works, I'm approaching a gargabe
collector written in assembler (with gnu as), and just wondered how
could I translate a virtual memory address to physical, so I can have
shared memory as well.

Though I've been browsing my system's header files in regards of the
shared memory subsytem, couldn't get to the bottom of it, so I wonder
if someone can gives us some clues.

Kind Regards,

Barry Margolin

2006-09-26, 7:00 pm

In article <1159293534.946232.115330@i3g2000cwc.googlegroups.com>,
"Chinlu" <chinluchinawa@yahoo.co.uk> wrote:

> Hi there,
>
> I've been looking at how shared memory works, I'm approaching a gargabe
> collector written in assembler (with gnu as), and just wondered how
> could I translate a virtual memory address to physical, so I can have
> shared memory as well.


You can't, unless you're running within the kernel. Shared memory is
implemented by the virtual memory subsystem of the kernel. The page
table registers that handle it behind the scenes can only be accessed
from privileged mode.

But you don't need to do that to get shared memory, you do it by calling
the appropriate system calls, like mmap() and shmget().

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Chinlu

2006-09-26, 7:00 pm

Barry Margolin wrote:
> In article <1159293534.946232.115330@i3g2000cwc.googlegroups.com>,
> "Chinlu" <chinluchinawa@yahoo.co.uk> wrote:
>
>
> You can't, unless you're running within the kernel. Shared memory is
> implemented by the virtual memory subsystem of the kernel. The page
> table registers that handle it behind the scenes can only be accessed
> from privileged mode.
>
> But you don't need to do that to get shared memory, you do it by calling
> the appropriate system calls, like mmap() and shmget().
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***


Hello Barry,

Yes, I acutally ended up by having to look my kernel sources', though I
thought this could be done from pure assembly, I'm not linking to c so
to use mmap or anyother libc facilitiy is not an option (or it
shouldn't be).

In the other hand one doesn't need to be root to use the functions you
say. Is this really impossible to get with just assembler while using
the linux kernel then?

Kind Regards,

Paul Pluzhnikov

2006-09-26, 7:00 pm

"Chinlu" <chinluchinawa@yahoo.co.uk> writes:

> I'm not linking to c so
> to use mmap or anyother libc facilitiy is not an option


Invoking mmap(2) (or any other) system call does *not* require
linking to libc, and can easily be done from assembly.

> Is this really impossible to get with just assembler while using
> the linux kernel then?


It is quite possible. Here is a trivial x86 example:

$ cat write.s
SYS_exit = 1
SYS_write = 4
..data
hello:
.string "hello world\n"
..text
..globl _start
_start:
movl $SYS_write,%eax
movl $1,%ebx # fd = fileno(stdio)
movl $hello,%ecx # buf
movl $12,%edx # count
int $0x80

movl $SYS_exit,%eax
xorl %ebx,%ebx
int $0x80
ret

$ gcc -nostdlib write.s && ./a.out
hello world

See also:
http://asm.sourceforge.net/articles/linasm.html

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Gordon Burditt

2006-09-27, 8:00 am

>Yes, I acutally ended up by having to look my kernel sources', though I
>thought this could be done from pure assembly, I'm not linking to c so
>to use mmap or anyother libc facilitiy is not an option (or it
>shouldn't be).


Even if you insist on your program being pure assembly language,
that doesn't necessarily rule out linking with the system calls in
libc (which may be written in assembly language).

>In the other hand one doesn't need to be root to use the functions you
>say. Is this really impossible to get with just assembler while using
>the linux kernel then?


If you insist on using only assembly language (why?), compile
anything you need in libc to link against with gcc -S, then consider
the result to be your new source code and link with it.


Nils O. Selåsdal

2006-09-27, 8:00 am

Chinlu wrote:
> Barry Margolin wrote:
>
> Hello Barry,
>
> Yes, I acutally ended up by having to look my kernel sources', though I
> thought this could be done from pure assembly, I'm not linking to c so
> to use mmap or anyother libc facilitiy is not an option (or it
> shouldn't be).

mmap and shmat is a feature of the kernel, not the C library.
The C library just has a wrapper for the syscall. Thus you can
call the syscall directly from assembly.

> In the other hand one doesn't need to be root to use the functions you
> say. Is this really impossible to get with just assembler while using
> the linux kernel then?

The linux kernel , as "all" unix kernels, provides an abstraction to
userland,
as well as protection - by the help of the hardware. Userland can't
perform certain operation, and it is restricted to access the info
you're after.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com