Re: i386 asm tutorial [was Re: copy_from_user() fix]

Jamie Lokier (lkd@tantalophile.demon.co.uk)
Sun, 30 Aug 1998 03:15:39 +0100


On Fri, Aug 28, 1998 at 01:12:11PM +0200, Andrea Arcangeli wrote:
> + "2:\n" \
> + ".section .fixup,\"ax\"\n" \
>
> Why to change section? And has it to be called fixup? And what does it
> mean ,"ax" at the end of the section declaration?

Fixups are described in linux/Documentation/exception.txt. The change
of section means what follows doesn't follow the previous code in
memory, and isn't executed normally.

In fact the .fixup code is executed when the earlier code page faults,
due to the fixup mechanism.

"ax" just marks .fixup as containing executable code, as opposed to,
say, read-only data.

> + "3: lea 0(%1,%0,4),%0\n" \
>
> Is this the addressing mode with base - index - scale - displacement
> explained at pag 90 of the Intel document 24319881?

Yes. AT&T syntax, what GNU tools use on i386.
Displacement = 0 (I'm not sure why that's there).
Base = %1. Index = %0. Scale = 4.

Here it's being used to do a simple sum, to store the total number of
bytes not yet copied into %0.

> + "4: pushl %0\n" \
> + " pushl %%eax\n" \
> + " xorl %%eax,%%eax\n" \
> + " rep; stosb\n" \

> This should continue zeroing the memory pointed by edi (but we have just
> written size bytes?) and btw I don' t know what ecx contains...

%%ecx is the same as %0. It contains the remaining unwritten bytes, by
virtue of the code being jumped to if the earlier movsl/movsb page
faults. See exception.txt.

I think it would be clearer to just use %%ecx in the macro.

> + ".section __ex_table,\"a\"\n" \
> + " .align 4\n" \
> + " .long 0b,3b\n" \
> + " .long 1b,4b\n" \
>
> And where does we use these numbers? Maybe in the unknown instruction
> (3:)?

These are local labels. 0b means "the previous label called 0", etc.
These add entries to __ex_table, which is part of the fixup mechanism.
They mean if a page fault happens at instruction 0b, jump to 3b to
handle it. If at 1b, jump to 4b to handle it.

> + : "=&c"(size) \
> ^ This is needed because we use string operation that
> autoread/autowrite ecx?

Yes. The "&" means %ecx can't be used as an input operand or part of
one (e.g. memory address), though it is probably redundant in this case.

Enjoy,
-- Jamie

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html