<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coder . cl &#187; freebsd</title>
	<atom:link href="http://coder.cl/category/programming/freebsd/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>system programmer &#38; web developer</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>foreign system call emulations on freebsd</title>
		<link>http://coder.cl/2009/07/foreign-system-call-emulations-on-freebsd/</link>
		<comments>http://coder.cl/2009/07/foreign-system-call-emulations-on-freebsd/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:28:17 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=83</guid>
		<description><![CDATA[Call simulations are common nowdays techniques to use foreign applications &#8212; build for other operating systems, such as M$ Windows &#8212; in the FreeBSD platform. The common technique is to create an interface to the real system calls replacing foreign system calls with wrappers on them. To build this task an assembler instruction is used. [...]]]></description>
			<content:encoded><![CDATA[<p><!--:en-->
<p>Call simulations are common nowdays techniques to use foreign applications &mdash; build for other operating systems, such as M$ Windows &mdash; in the FreeBSD platform. The common technique is to create an interface to the real system calls replacing foreign system calls with wrappers on them. To build this task an <i>assembler instruction</i> is used. On call simulations we have the usage of <i>win32 codecs</i>, <i>wine</i> and <i>valgrind</i> &mdash; valgrind do not use foreign system calls, but replaces standard library routines. Most of them are using &mdash; behind all those system calls and standard library routines &mdash; a common <i>assembler instruction</i>: <b>lldt</b>. <b>lldt</b> stands for <i>&quot;Load Local Descriptor Table&quot;</i> and it&#8217;s related to <i>Segment Descriptor Tables</i>. This article is a lightweight introduction to the use of <b>lldt</b> assembler instruction.</p>
<p><!--:--><span id="more-83"></span><!--:en--></p>
<blockquote><p>
LLDT loads the Local Descriptor Table register (LDTR). The word operand (memory or register) to LLDT should contain a selector to the <a href="http://wiki.osdev.org/Global_Descriptor_Table">Global Descriptor Table</a> (GDT). The <a href="http://wiki.osdev.org/GDT_Tutorial">GDT</a> entry should be a Local Descriptor Table. If so, then the LDTR is loaded from the entry. The descriptor registers DS, ES, SS, FS, GS, and CS are not affected. The LDT field in the <a href="http://wiki.osdev.org/TSS">task state segment</a> does not change.</p>
<p>The selector operand can be 0; if so, the LDTR is marked invalid. All descriptor references (except by the LAR, VERR, VERW or LSL instructions) cause a #GP fault.</p>
<p>LLDT is used in operating system software; it is not used in application programs.
</p></blockquote>
<p>Thanks to this <i>instruction set</i> &mdash; specific for <a href="http://www.intel.com/">Intel</a> architecture &mdash; each task can have it&#8217;s own <i>LDT</i>. This instructions only works in <a href="http://wiki.osdev.org/Protected_mode">Protected Mode</a>, and seems to be made specially for <i>virtual addressing</i> purposes. This means that each task with an LDT in Protected Mode, can have it&#8217;s own memory segment, call gates, an so.</p>
<h3>usage on valgrind under freebsd</h3>
<p><a href="http://valgrind.org/">valgrind</a> makes a call to <a href="http://www.freebsd.org/cgi/man.cgi?query=i386_set_ldt&#038;apropos=0&#038;sektion=0&#038;manpath=FreeBSD+7.2-RELEASE&#038;format=html">i386_set_ldt(2)</a> on <a href="http://www.freebsd.org/">FreeBSD</a>.</p>
<blockquote><p>
The i386_get_ldt() system call returns a list of the i386 descriptors in the current process&#8217; LDT. The i386_set_ldt() system call sets a list of i386 descriptors in the current process&#8217; LDT. For both routines, start_sel specifies the index of the selector in the LDT at which to begin and descs points to an array of num_sels descriptors to be set or returned.
</p></blockquote>
<pre name="code" class="c" cols="80" rows="43">
/* the descriptor union (union and not struct for alignment) */
union descriptor ldt;

/* calculate the page count using the process base address and it's
    ending address where VKI_BYTES_PER_PAGE is defined as (1 &lt;&lt; 12)*/
UInt limit = (VG_(client_end)-VG_(client_base)) / VKI_BYTES_PER_PAGE;

Int ret;

/* apply zeroes to the descriptor <img src='http://coder.cl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  */
memset(&amp;ldt, 0, sizeof(ldt));

/* set limit to the <a href="http://en.wikipedia.org/wiki/Least_significant_bit">lsb extent</a> en ensures it's bits */
ldt.sd.sd_lolimit = limit &amp; 0xffff;

/* set limit to the <a href="http://en.wikipedia.org/wiki/Least_significant_bit">lsb base</a> en ensures it's bits */
ldt.sd.sd_lobase = VG_(client_base) &amp; 0xffffff;

/* this is important! set the access for the described memory
   segment to read/write access, that's because the machine
   byte code is loaded into the described segments. */
ldt.sd.sd_type = SDT_MEMRWA;

/* set the user priority to 3 */
ldt.sd.sd_dpl = SEL_UPL;

/* set to be present, the kernel check only for ring-3 (SEL_UPL)
   to be present:
    if ((dp->sd.sd_p != 0) &amp;&amp; (dp->sd.sd_dpl != SEL_UPL))
        return (EACCES); */
ldt.sd.sd_p = 1;      /* present */

/* set the upper limit for <a href="http://en.wikipedia.org/wiki/Most_significant_bit">msb extent</a> */
ldt.sd.sd_hilimit = (limit &gt;&gt; 16) &amp; 0xf;

/* user 32 bit addressing */
ldt.sd.sd_def32 = 1;  /* 32 bit */

/* set granularity to 1 page */
ldt.sd.sd_gran = 1;   /* limit in pages */

/* set the upper limit for <a href="http://en.wikipedia.org/wiki/Most_significant_bit">msb base</a> */
ldt.sd.sd_hibase = (VG_(client_base) &gt;&gt; 24) &amp; 0xff;

/* and finally do the system call <b>i386_set_ldt(2)</b> */
ret = i386_set_ldt(VG_POINTERCHECK_SEGIDX, &amp;ldt, 1);
</pre>
<p>And then. What does the <b>i386_set_ldt(2)</b> call do?. The call is made through <a href="http://www.freebsd.org/cgi/man.cgi?query=sysarch&#038;apropos=0&#038;sektion=0&#038;manpath=FreeBSD+7.2-RELEASE&#038;format=html">sysarch(2)</a> system call and directly <b>i386_set_ldt(2)</b>. It take three arguments, the starting <i>LDT</i> index <i>start</i>, a pointer to the descriptors array <i>descs</i> and the number of descriptors to process <i>num</i>. What is inside the call?. The sysarch takes the switch from the call to <i>sysarch(I386_SET_LDT, &amp;p);</i>, where <i>p</i> is a struct of <i>i386_ldt_args</i>, that holds the three arguments and passes to the <b>sysarch(2)</b> system call. After copying the arguments from <i>user space</i> to <i>kernel space</i>, the magic happens and the <i>kernel interface</i> <b>i386_set_ldt</b> is called. Here two locks occurs, first, after building the switch on the user arguments and sending them to the <i>kernel space</i>, the <i>Giant lock</i> is acquired &mdash; <img src='http://coder.cl/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  yes, that&#8217;s true on FreeBSD versions prior to 7.X. Then, on the <i>kernel interface</i> <b>i386_set_ldt</b>, the array of <i>LDT descriptors</i> are validated, in such form that the are checked for invalid types, as <a href="http://wiki.osdev.org/TSS">TSS</a> access and memory segment types &mdash; both of them must not be accessed from user space, because are designed for <i>operating system</i> purposes. Then, the priority and present verification is done: <i>if ((dp->sd.sd_p != 0) &amp;&amp; (dp->sd.sd_dpl != SEL_UPL))</i>, this verifies that the <i>present flag</i> is set to <i>1</i> &mdash; one bit bitfield &mdash; and the priority is set to the <i>ring-3</i> or <i>user level</i>. It it does not fails, the <i>scheduler spin lock</i> is acquired and the LDT is grown through <b>i386_ldt_grow</b> kernel interface. Here, the <b>set_user_ldt</b> kernel interface is called. What is <b>set_user_ldt</b>? The call to the <b>lldt</b> instruction!</p>
<pre name="code" class="c" cols="80" rows="43">
/* sys/i386/include/cpufunc.h */
static __inline void
lldt(u_short sel)
{
    __asm __volatile(&quot;lldt %0&quot; : : &quot;r&quot; (sel));
}
</pre>
<p>And how it is called?</p>
<pre name="code" class="c" cols="80" rows="43">
/*
 * Update the GDT entry pointing to the LDT to point to the LDT of the
 * current process.
 *
 * This must be called with sched_lock held.  Unfortunately, we can't use a
 * mtx_assert() here because cpu_switch() calls this function after changing
 * curproc but before sched_lock's owner is updated in mi_switch().
 */
void
set_user_ldt(struct mdproc *mdp)
{
    struct proc_ldt *pldt;

    pldt = mdp->md_ldt;
#ifdef SMP
    gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
#else
    gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
#endif
    lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
    PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
}
</pre>
<p>Here the call to <b>set_user_ldt</b> is made through <b>smp_rendezvous</b> on SMP kernels and directly on non-SMP kernels. On SMP kernels, a <a href="http://wiki.osdev.org/Global_Descriptor_Table">GDT</a> is selected using the CPU ID selector, that uses the <i>CPU variable name</i> for the current process and places the <i>address</i> of the <i>LDT segment descriptor</i> in the <i>GDT</i> &mdash; as a descriptor table selection process &mdash; and then calls the <a href="http://pdos.csail.mit.edu/6.828/2005/readings/i386/LLDT.htm">lldt</a> instruction with that address on the GDT. Then, the per CPU <i>currentldt</i> to work on, is selected through <tt>PCPU_SET</tt> macro and the magic is done. On a single CPU system, the unique CPU on it will always be selected by default. Also, <a href="http://pdos.csail.mit.edu/6.828/2005/readings/i386/LLDT.htm">lldt</a> is called on task switching. But task switch it&#8217;s something for other post, mainly because it regards the <i>Process Scheduler</i> and similar topics.</p>
<p>For a while, the code on the task switching code is bellow and in short, the call is made to select the default LDT if it differs from the current LDT:</p>
<pre><code>
movl    _default_ldt,%eax
cmpl    PCPU(CURRENTLDT),%eax
je  2f
lldt    _default_ldt
movl    %eax,PCPU(CURRENTLDT)
jmp 2f
</code></pre>
<p></p>
<h3>the usage on wine under freebsd</h3>
<p><a href="http://www.winehq.org/">Wine</a> have not much differences on the usage than <b>valgrind</b>. The main difference is that Wine define it&#8217;s own LDT structure &mdash; and it&#8217;s required by the Win32 API, the same one with different declaration &mdash; as follows:</p>
<pre name="code" class="c" cols="80" rows="43">
typedef struct _LDT_ENTRY {
    WORD    LimitLow;
    WORD    BaseLow;
    union {
        struct {
            BYTE    BaseMid;
            BYTE    Flags1;
            BYTE    Flags2;
            BYTE    BaseHi;
        } Bytes;
        struct {
            /* this comment is mine: WTF?!?! Pascal case mixed
               with underscore!!! The code looks really ugly. */
            unsigned    BaseMid: 8;
            unsigned    Type : 5;
            unsigned    Dpl : 2;
            unsigned    Pres : 1;
            unsigned    LimitHi : 4;
            unsigned    Sys : 1;
            unsigned    Reserved_0 : 1;
            unsigned    Default_Big : 1;
            unsigned    Granularity : 1;
            unsigned    BaseHi : 8;
        } Bits;
    } HighWord;
#ifdef _WIN64  /* FIXME: 64-bit code should not be using the LDT */
    DWORD BaseHigh;
#endif
} LDT_ENTRY, *PLDT_ENTRY;
</pre>
<p>Then, the call is made verifying &mdash; as the kernel does? &mdash; that the LDT have enabled the <i>present</i> flag and the <i>priority</i> is set to the ring-3. Before calling it, it does a copy of the required LDT for running the <i>Windows Process</i>.</p>
<pre name="code" class="c" cols="80" rows="43">
LDT_ENTRY entry_copy = *entry;
/* The kernel will only let us set LDTs with user priority level */
if (entry_copy.HighWord.Bits.Pres
    &amp;&amp; entry_copy.HighWord.Bits.Dpl != 3)
    entry_copy.HighWord.Bits.Dpl = 3;
    ret = i386_set_ldt(index, (union descriptor *)&amp;entry_copy, 1);
    if (ret &lt; 0)
    {
        perror("i386_set_ldt");
        fprintf( stderr, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
        exit(1);
    }
}
</pre>
<p>I remember a <i>modification</i> made from some compilers that were using a <i>fixed user space address</i> on Windows to load the <i>entry point</i>, instead of <i>calculating</i> it, before the LDT was set, crashing with a <i>segfault</i> signal and <i>Invalid address</i> message. Possibly in <a href="http://bugs.winehq.org/show_bug.cgi?id=16023">this thread</a> and <a href="http://wiki.jswindle.com/index.php/Free_BSD">better explained problem</a>.</p>
<blockquote><p>
The main reason the reservation code is still disabled on FreeBSD is because mmap(NULL) only tries addresses after the executable + some malloc heap space. Wine is located at 0x7bf00000. The heap size is currently set to 0&#215;02000000 by the wine-freebsd loader, which is thought to be the absolute minimum required to support FreeBSD 6.</p></blockquote>
<p></p>
<h3>linux emulation layer on freebsd</h3>
<p>This layer of emulation implements <b>modify_ldt(2)</b> system call. The call implementation is done through <b>linux_modify_ldt</b> kernel interface. Here, the code is simpler. Just acquire the Giant Lock and releases it after calling the kernel interface <b>i386_set_ldt</b> &mdash; not the system call &mdash; and it sets the <tt>SDT_MEMRO</tt> type!. This means that every memory block referenced by the <i>segment descriptor</i> is <i>read only</i>.</p>
<pre name="code" class="c" cols="80" rows="43">
desc.sd.sd_lolimit = (ld.limit &amp; 0x0000ffff);
desc.sd.sd_hilimit = (ld.limit &amp; 0x000f0000) >> 16;
desc.sd.sd_lobase = (ld.base_addr &amp; 0x00ffffff);
desc.sd.sd_hibase = (ld.base_addr &amp; 0xff000000) &gt;&gt; 24;
desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) &lt;&lt; 1) | (ld.contents &lt;&lt; 2);
desc.sd.sd_dpl = 3;
desc.sd.sd_p = (ld.seg_not_present ^ 1);
desc.sd.sd_xx = 0;
desc.sd.sd_def32 = ld.seg_32bit;
desc.sd.sd_gran = ld.limit_in_pages;
mtx_lock(&amp;Giant);
error = i386_set_ldt(td, &amp;ldt, &amp;desc);
mtx_unlock(&amp;Giant);
</pre>
<p>The manual page for <b>modify_ldt(2)</b> on Linux says that you can not call it directly, you must use <b>syscall(2)</b> instead, and it is not made though a system specific call such as <b>sysarch(2)</b>.</p>
<blockquote><p>
modify_ldt() reads or writes the local descriptor table (ldt) for a process. The ldt is a per-process memory management table used by the i386 processor. For more information on this table, see an Intel 386 processor handbook.
</p></blockquote>
<p><!--:--></p>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;title=foreign%20system%20call%20emulations%20on%20freebsd&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=Call%20simulations%20are%20common%20nowdays%20techniques%20to%20use%20foreign%20applications%20%26mdash%3B%20build%20for%20other%20operating%20systems%2C%20such%20as%20M%24%20Windows%20%26mdash%3B%20in%20the%20FreeBSD%20platform.%20The%20common%20technique%20is%20to%20create%20an%20interface%20to%20the%20real%20system%20calls%20replacin" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;title=foreign%20system%20call%20emulations%20on%20freebsd&amp;notes=Call%20simulations%20are%20common%20nowdays%20techniques%20to%20use%20foreign%20applications%20%26mdash%3B%20build%20for%20other%20operating%20systems%2C%20such%20as%20M%24%20Windows%20%26mdash%3B%20in%20the%20FreeBSD%20platform.%20The%20common%20technique%20is%20to%20create%20an%20interface%20to%20the%20real%20system%20calls%20replacin" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;title=foreign%20system%20call%20emulations%20on%20freebsd&amp;bodytext=Call%20simulations%20are%20common%20nowdays%20techniques%20to%20use%20foreign%20applications%20%26mdash%3B%20build%20for%20other%20operating%20systems%2C%20such%20as%20M%24%20Windows%20%26mdash%3B%20in%20the%20FreeBSD%20platform.%20The%20common%20technique%20is%20to%20create%20an%20interface%20to%20the%20real%20system%20calls%20replacin" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;title=foreign%20system%20call%20emulations%20on%20freebsd" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;t=foreign%20system%20call%20emulations%20on%20freebsd" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=foreign%20system%20call%20emulations%20on%20freebsd%20-%20http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2009%2F07%2Fforeign-system-call-emulations-on-freebsd%2F&amp;title=foreign%20system%20call%20emulations%20on%20freebsd&amp;annotation=Call%20simulations%20are%20common%20nowdays%20techniques%20to%20use%20foreign%20applications%20%26mdash%3B%20build%20for%20other%20operating%20systems%2C%20such%20as%20M%24%20Windows%20%26mdash%3B%20in%20the%20FreeBSD%20platform.%20The%20common%20technique%20is%20to%20create%20an%20interface%20to%20the%20real%20system%20calls%20replacin" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2009 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2009. | <a href="http://coder.cl/2009/07/foreign-system-call-emulations-on-freebsd/">Permalink</a> | <a href="http://coder.cl/2009/07/foreign-system-call-emulations-on-freebsd/#comments">One comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/07/foreign-system-call-emulations-on-freebsd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
