1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<title>Porting UML to a new architecture</title>
</head>
<body alink="#FF0000" vlink="#55188A" link="#0000EF" bgcolor="#FFFFFF" text="#000099">
<table border="0">
<tr align="left">
<td valign="top">
<table border="0">
<tr align="left"><td valign="top" >
<img width="120" height="171" src="/uml-small.png">
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/index.html">Site Home Page</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/uses.html">What it's good for</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/case-studies.html">Case Studies</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/kernel.html">Kernel Capabilities</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/dl-sf.html">Downloading it</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/run.html">Running it</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/compile.html">Compiling</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/install.html">Installation</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/fs_making.html">Building filesystems</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/faq.html">Troubles</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/contrib.html">User Contributions</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/links.html">Related Links</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/todo.html">The ToDo list</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/projects.html">Projects</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/diary.html">Diary</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/thanks.html">Thanks</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/contacts.html">Contacts</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Tutorials<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/UserModeLinux-HOWTO.html">The HOWTO (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/UserModeLinux-HOWTO.txt">The HOWTO (text)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/hostfs.html">Host file access</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/input.html">Device inputs</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/shared_fs.html">Sharing filesystems</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/fs.html">Creating filesystems</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/networking.html">Virtual Networking</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/mconsole.html">Management Console</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/debugging.html">Kernel Debugging</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/gprof.html">gprof and gcov</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/xtut.html">Running X</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/trouble.html">Diagnosing problems</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/config.html">Configuration</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slack_readme.html">Installing Slackware</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/arch-port.html">Porting UML</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/iomem.html">IO memory emulation</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">How you can help<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/help-gen.html">Overview</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/help-doc.html">Documentation</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/help-userspace.html">Utilities</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/help-kernel-v1.html">Kernel bugs</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/projects.html">Kernel projects</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Screenshots<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/net.html">A virtual network</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/x.html">An X session</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Transcripts<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/login.html">A login session</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/debug-session.html">A debugging session</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slackinst.html">Slackware installation</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Reference<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/switches.html">Kernel switches</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slack_readme.html">Slackware README</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Papers<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/als2000/index.html">ALS 2000 paper (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/als2000.tex">ALS 2000 paper (TeX)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/als2000/slides.html">ALS 2000 slides</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/lca2001/lca.html">LCA 2001 slides</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/ols2001/index.html">OLS 2001 paper (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/ols2001.tex">OLS 2001 paper (TeX)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/als2001/index.html">ALS 2001 paper (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/als2001.tex">ALS 2001 paper (TeX)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/ists2002/umlsec.htm">UML security (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/lca2002/lca2002.htm">LCA 2002 (html)</a></font>
</td></tr>
<tr align="left"><td valign="top" bgcolor="#e0e0e0">Fun and Games<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/http://user-mode-linux.sourceforge.net/cgi-bin/hangman">Kernel Hangman</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/sdotm.html">Disaster of the Month</a></font>
</td></tr>
</table>
</td>
<td valign="top" align="left">
<center>
<h3>Porting UML to a new architecture</h3>
</center>
Even though UML is running on a host Linux, which insulates it from
the underlying platform to a great extent, some details of the hardware
still leak through and make porting UML to Linux on a new architecture
more than a simple rebuild.
<p>
The major aspects of the hardware that show through are
<ul>
<li>
register names used by ptrace
</li>
<li>
organization of the process address space
</li>
</ul>
<p>
This page will describe how to port UML to a new architecture. It
will acquire new material as we learn more about how to do it. At
this point, this is based on what we learned from the ppc port, which
is the only real port of UML that's been done so far. The i386 port
doesn't really count since that was part of the overall development of
UML rather than a separate porting effort.
<p>
Below, there are references to $(SUBARCH). This is the make variable
which holds the value of the host architecture in the UML build. On
Intel boxes, it's "i386" and on PowerPC boxes, it's "ppc".
<a name="Overview"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Overview</font>
</b>
</td>
</tr>
</table>
<blockquote head="Overview">
UML is split between architecture-independent code and headers which
are found under arch/um in
<ul>
<li>
kernel
</li>
<li>
drivers
</li>
<li>
fs
</li>
<li>
ptproxy
</li>
<li>
include
</li>
</ul>
and the architecture-dependent code and definitions under arch/um in
<ul>
<li>
Makefile-*
</li>
<li>
sys-*
</li>
<li>
include/sysdep-*
</li>
</ul>
Each '*' is the name of an architecture, so the i386-specific code is
under arch/um/sys-i386 and the ppc-specific code is under arch/um/sys-ppc.
<p>
</blockquote>
<a name="The host"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">The host</font>
</b>
</td>
</tr>
</table>
<blockquote head="The host">
Not all architectures can currently run UML. The potential problem is
the ability of ptrace to change system call numbers. i386 couldn't
until I got the change into 2.3.22 and 2.2.15, ppc could, and IA64 and
mips can't. I don't know about the other arches.
<p>
This is necessary because it's critical to UML's ability to virtualize
system calls. Process system calls must be nullified in the host, and
this is done by converting them into getpid.
<p>
So, before starting to work on your new port of UML, make sure ptrace
is fully functional. <a href="examples/ptrace_test.c">This
little program</a> starts a child, which makes calls to getpid and
prints them out, while the parent is converting the getpid calls to
getppid. The parent prints out its own pid, while the child prints out
what it thinks is its own pid, and they should be the same. So, if
your machine is able to run UML, you will see output like this:
<tt>
<pre>
<font size="-1">
Parent pid = 3246
getpid() returned 3246
getpid() returned 3246
</font>
</pre>
</tt>
If not, you will likely get errors from ptrace. Less likely is
different pids being printed out from the two processes. If either
happens, then you need to figure out how to remove that restriction
from the host Linux.
<p>
Note that when you compile ptrace.c, you will need to change the
references to ORIG_EAX, which contains the system call number, to
whatever is appropriate for your architecture.
</blockquote>
<a name="Address space layout"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Address space layout</font>
</b>
</td>
</tr>
</table>
<blockquote head="Address space layout">
Before delving into the code, you need to do some high-level
conceptual thinking about how to organize the address space of a UML
process. UML maps its executable, physical memory, and kernel virtual
memory into the address space of each of its processes. You need to
decide where to put each of these so as to minimize the likelihood of
a process trying to allocate that memory for its own use.
<p>
The only arch hook at this point is where in the address space the UML
binary is going to load. The other addresses are still hard-coded
because they happen to work for both i386 and ppc. UML puts its own
memory in the area starting at 0x5000000 and process stacks 4M below
its own process stack. These choices may not work on all
architectures, so feel free to generalize them. To locate a likely
area on your arch, staring at /proc/<pid>/maps of various processes
on the host has been the technique so far.
</blockquote>
<a name="Architecture Makefile"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Architecture Makefile</font>
</b>
</td>
</tr>
</table>
<blockquote head="Architecture Makefile">
You need to create arch/um/Makefile-$(SUBARCH), which contains the
following definitions:
<ul>
<li>
START_ADDR - The address where the UML executable will load in
memory. This address must be chosen so that it won't conflict with
any memory that a UML process is going to want to use. The i386
definition is
<tt>
<pre>
<font size="-1">START_ADDR = 0x10000000</font>
</pre>
</tt>
</li>
<li>
ARCH_CFLAGS - Anything that needs to be added to CFLAGS goes here.
Both the i386 and ppc ports use this to turn off definitions that
would pull hardware-specific code into the kernel. The ppc definition
is
<tt>
<pre>
<font size="-1">ARCH_CFLAGS = -U__powerpc__ -D__UM_PPC__</font>
</pre>
</tt>
</li>
<li>
ELF_SUBARCH - This is the name of the ELF object format for the
architecture. On i386, it's 'i386', but on ppc, it's not 'ppc' (it's
'powerpc'). The i386 definition is
<tt>
<pre>
<font size="-1">ELF_SUBARCH = $(SUBARCH)</font>
</pre>
</tt>
</li>
</ul>
</blockquote>
<a name="Architecture headers"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Architecture headers</font>
</b>
</td>
</tr>
</table>
<blockquote head="Architecture headers">
There are three headers, which go in arch/um/include/sysdep-$(SUBARCH),
which need to be written, and each needs to
contain a certain set of definitions.
<p>
<b>ptrace.h</b>
<p>
ptrace.h contains a number of definitions which insulate generic UML
code, and its calls to ptrace, from details of the machine's register
set.
<p>
First, the register set needs to be defined as struct sys_pt_regs.
This is a structure which contains the machine's register
set as defined by ptrace. This may be different from the machine's
register set - i386 adds ORIG_EAX, which doesn't correspond to a
register on the chip. The generic code won't look inside the
structure directly, but will do structure copies and access it through
the macros described below. The i386 definition is
<tt>
<pre>
<font size="-1">
#define UM_MAX_REG (17)
struct sys_pt_regs {
unsigned long regs[UM_MAX_REG];
};
</font>
</pre>
</tt>
Along with this, you need a definition of EMPTY_REGS, which is a
default initialization of a struct sys_pt_regs:
<tt>
<pre>
<font size="-1">
#define EMPTY_REGS { { [ 0 ... UM_MAX_REG - 1 ] = 0 } }
</font>
</pre>
</tt>
<p>
Then, you need a number of macros to allow the generic code to
access information in a register set. These come in two
varieties, UM_* and UM_*_OFFSET. The UM_* macros return a field from a
struct sys_pt_regs which can be assigned to or its value taken, while
the UM_*_OFFSET macros return a number suitable for a call to
ptrace(PTRACE_SETREG, ...). Below are the required macros and their
i386 definitions.
<ul>
<li>
UM_IP(regs) - the instruction pointer from regs
</li>
<li>
UM_IP_OFFSET - the instruction pointer offset
<tt>
<pre>
<font size="-1">
#define UM_REG(r, n) ((r)->regs[n])
#define UM_IP(r) UM_REG(r, EIP)
#define UM_IP_OFFSET (EIP * sizeof(long))
</font>
</pre>
</tt>
</li>
<li>
UM_SP(regs) - the stack pointer from regs
</li>
<li>
UM_SP_OFFSET - the stack pointer offset
<tt>
<pre>
<font size="-1">
#define UM_SP(r) UM_REG(r, UESP)
#define UM_SP_OFFSET (UESP * sizeof(long))
</font>
</pre>
</tt>
</li>
<li>
UM_ELF_ZERO(regs)
</li>
<li>
UM_ELF_ZERO_OFFSET - the register and offset that needs to be zeroed
before branching to the entry point of a new process. This is a bit
of a kludge and may be replaced with a more general UM_ELF_INIT(regs)
macro.
<tt>
<pre>
<font size="-1">
#define UM_ELF_ZERO(r) UM_REG(r, EDX)
#define UM_ELF_ZERO_OFFSET (EDX * sizeof(long))
</font>
</pre>
</tt>
</li>
<li>
UM_FIX_EXEC_STACK - A hook which is called after
loading an ELF image into memory. On i386, this is empty, but on ppc,
for example, the stack needs to be moved to sit on a 16 byte
boundary.
<tt>
<pre>
<font size="-1">
extern void shove_aux_table(unsigned long sp);
#define UM_FIX_EXEC_STACK(sp) shove_aux_table(sp);
</font>
</pre>
</tt>
</li>
<li>
UM_SYSCALL_RET(regs)
</li>
<li>
UM_SYSCALL_RET_OFFSET - the register and offset that contains the
system call return value as it finishes.
<tt>
<pre>
<font size="-1">
#define UM_SYSCALL_RET(r) UM_REG(r, EAX)
#define UM_SYSCALL_RET_OFFSET (EAX * sizeof(long))
</font>
</pre>
</tt>
</li>
<li>
UM_SYSCALL_NR(regs)
</li>
<li>
UM_SYSCALL_NR_OFFSET - the register and offset that contains the system call
number at the start of system call execution.
<tt>
<pre>
<font size="-1">
#define UM_SYSCALL_NR(r) UM_REG(r, ORIG_EAX)
#define UM_SYSCALL_NR_OFFSET (ORIG_EAX * sizeof(long))
</font>
</pre>
</tt>
</li>
<li>
UM_SET_SYSCALL_RETURN - stores the result of a system call in the right place
in the register set. On some architectures special handling is
needed, eg setting a flag on error returns on ppc.
<tt>
<pre>
<font size="-1">
#define UM_SET_SYSCALL_RETURN(_regs, result) \
do { \
if (result < 0) { \
(_regs)->regs[PT_CCR] |= 0x10000000; \
UM_SYSCALL_RET((_regs)) = -result; \
} else { \
UM_SYSCALL_RET((_regs)) = result; \
} \
} while(0)
</font>
</pre>
</tt>
</li>
<li>
UM_SYSCALL_ARG1(regs)
</li>
<li>
UM_SYSCALL_ARG1_OFFSET
</li>
<li>
UM_SYSCALL_ARG2(regs)
</li>
<li>
UM_SYSCALL_ARG2_OFFSET
</li>
<li>
UM_SYSCALL_ARG3(regs)
</li>
<li>
UM_SYSCALL_ARG3_OFFSET
</li>
<li>
UM_SYSCALL_ARG4(regs)
</li>
<li>
UM_SYSCALL_ARG4_OFFSET
</li>
<li>
UM_SYSCALL_ARG5(regs)
</li>
<li>
UM_SYSCALL_ARG5_OFFSET
</li>
<li>
UM_SYSCALL_ARG6(regs)
</li>
<li>
UM_SYSCALL_ARG6_OFFSET - the appropriate system call argument or offset
<tt>
<pre>
<font size="-1">
#define UM_SYSCALL_ARG1(r) UM_REG(r, EBX)
#define UM_SYSCALL_ARG2(r) UM_REG(r, ECX)
#define UM_SYSCALL_ARG3(r) UM_REG(r, EDX)
#define UM_SYSCALL_ARG4(r) UM_REG(r, ESI)
#define UM_SYSCALL_ARG5(r) UM_REG(r, EDI)
#define UM_SYSCALL_ARG6(r) UM_REG(r, EBP)
#define UM_SYSCALL_ARG1_OFFSET (EBX * sizeof(long))
#define UM_SYSCALL_ARG2_OFFSET (ECX * sizeof(long))
#define UM_SYSCALL_ARG3_OFFSET (EDX * sizeof(long))
#define UM_SYSCALL_ARG4_OFFSET (ESI * sizeof(long))
#define UM_SYSCALL_ARG5_OFFSET (EDI * sizeof(long))
#define UM_SYSCALL_ARG6_OFFSET (EBP * sizeof(long))
</font>
</pre>
</tt>
</li>
</ul>
<p>
<b>syscalls.h</b>
<p>
syscalls.h needs to define a number of things related to system calls.
<ul>
<li>
syscall_handler_t - a typedef describing the interface to the system
call functions. The i386 port declares a syscall_handler_t as taking
a struct sys_pt_regs as its only argument
<tt>
<pre>
<font size="-1">
typedef long syscall_handler_t(struct sys_pt_regs regs);
</font>
</pre>
</tt>
and the ppc port declares it as taking six unsigned longs
<tt>
<pre>
<font size="-1">
typedef long syscall_handler_t(unsigned long arg1, unsigned long arg2,
unsigned long arg3, unsigned long arg4,
unsigned long arg5, unsigned long arg6);
</font>
</pre>
</tt>
</li>
<li>
EXECUTE_SYSCALL - a macro which takes a system call number and a
register set and invokes the system call. This should match the
declaration of syscall_handler_t, so the i386 port sticks the entire
register set on the stack
<tt>
<pre>
<font size="-1">
#define EXECUTE_SYSCALL(syscall, regs) (*sys_call_table[syscall])(regs)
</font>
</pre>
</tt>
while the ppc port extracts the individual arguments
<tt>
<pre>
<font size="-1">
#define EXECUTE_SYSCALL(syscall, regs) \
(*sys_call_table[syscall])(UM_SYSCALL_ARG1(&regs), \
UM_SYSCALL_ARG2(&regs), \
UM_SYSCALL_ARG3(&regs), \
UM_SYSCALL_ARG4(&regs), \
UM_SYSCALL_ARG5(&regs), \
UM_SYSCALL_ARG6(&regs))
</font>
</pre>
</tt>
</li>
<li>
ARCH_SYSCALLS - a fragment of an array initialization. This will be
incorporated into the system call vector, which has the entry point
for each system call in the appropriate slot. This defines system
calls which are present on Linux for this architecture, but not for
all of the others. An empty value is OK, and the ppc port used to define
this as such. When it is not empty, it should be written with a
trailing comma. Here is a piece of the i386 definition:
<tt>
<pre>
<font size="-1">
#define ARCH_SYSCALLS \
[ __NR_vm86old ] = sys_ni_syscall, \
[ __NR_modify_ldt ] = sys_modify_ldt, \
[ __NR_lchown32 ] = sys_lchown, \
[ __NR_madvise ] = sys_madvise,
</font>
</pre>
</tt>
</li>
<li>
LAST_SYSCALL - this is the number of the last defined system call for
this architecture, which may be one of the generic system calls, so
check the numbers. It is used to pad out the otherwise undefined end
part of the system call vector.
<tt>
<pre>
<font size="-1">
#define LAST_SYSCALL __NR_madvise
</font>
</pre>
</tt>
</li>
</ul>
<p>
<b>sigcontext.h</b>
<p>
sigcontext.h defines a few sigcontext access macros.
<ul>
<li>
UM_ALLOCATE_SC(name) - Declares a sigcontext struct and any other data
which is part of a sigcontext. It's put at the end of the locals
where it's used, so it may also initialize things. The ppc definition
declares a separate register set and assigns a sigcontext field to
point to it:
<tt>
<pre>
<font size="-1">
#define UM_ALLOCATE_SC(name) \
struct sys_pt_regs name##_regs; \
struct sigcontext name; \
name.regs = &name##_regs
</font>
</pre>
</tt>
This will be filled in by fill_in_sigcontext, which has to match
whatever is set up here.
</li>
<li>
SC_FAULT_ADDR(sc) - Extracts the page fault address from the sigcontext
<tt>
<pre>
<font size="-1">
#define SC_FAULT_ADDR(sc) ((sc)->cr2)
</font>
</pre>
</tt>
</li>
<li>
SC_FAULT_WRITE(sc) - Zero if the fault was a read, non-zero if it was a write
<tt>
<pre>
<font size="-1">
#define SC_FAULT_WRITE(sc) (((sc)->err) & 2)
</font>
</pre>
</tt>
</li>
<li>
SC_IP(sc) - Extracts the faulting ip from the sigcontext
<tt>
<pre>
<font size="-1">
#define SC_IP(sc) ((sc)->eip)
</font>
</pre>
</tt>
</li>
<li>
SC_SP(sc) - Extracts the stack pointer from the sigcontext
<tt>
<pre>
<font size="-1">
#define SC_SP(sc) ((sc)->esp_at_signal)
</font>
</pre>
</tt>
</li>
<li>
SEGV_IS_FIXABLE(sc) - Returns non-zero or zero, depending on whether a
particular seg fault was caused by a page or protection fault, which
can be fixed, or some other fault, and can't. In the latter case, the
UML process will be segfaulted.
<tt>
<pre>
<font size="-1">
#define SEGV_IS_FIXABLE(sc) (((sc)->trapno == 14) || ((sc)->trapno == 13))
</font>
</pre>
</tt>
</li>
</ul>
</blockquote>
<a name="Port implementation"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Port implementation</font>
</b>
</td>
</tr>
</table>
<blockquote head="Port implementation">
The actual implementation of the port is contained in sys-$(SUBARCH).
You have complete freedom in this directory, except that when it is
built, it must produce an object file named sys.o which contains all
the code required by the generic kernel.
<p>
Here is a list of the files used by the existing ports, along with
what they define.
<p>
<b>ptrace.c</b>
<tt>
<pre>
<font size="-1">
int putreg(struct task_struct *child, unsigned long regno, unsigned long value)
</font>
</pre>
</tt>
This does any needed validity checking on the register and the value,
and assigns the value to the appropriate register in
child->thread.process_regs. If it fails, it returns -EIO.
<p>
This may be changed in the future so that it is provided with just the
register set rather than the whole task structure.
<tt>
<pre>
<font size="-1">
unsigned long getreg(struct task_struct *child, unsigned long regno)
</font>
</pre>
</tt>
getreg fetches the value of the requested register from
child->thread.process_regs, doing any required masking of registers
which don't use all their bits. This may also be changed to take the
register set rather than the task structure.
<p>
<b>ptrace_user.c</b>
<p>
Linux doesn't implement PTRACE_SETREGS and PTRACE_GETREGS on all
architectures. This file contains definitions of ptrace_getregs and
ptrace_setregs to hide this difference from the generic code.
Architectures which define PTRACE_SETREGS and PTRACE_GETREGS will
implement these functions as follows
<tt>
<pre>
<font size="-1">
int ptrace_getregs(long pid, struct sys_pt_regs *regs_out)
{
return(ptrace(PTRACE_GETREGS, pid, 0, regs_out));
}
int ptrace_setregs(long pid, struct sys_pt_regs *regs)
{
return(ptrace(PTRACE_SETREGS, pid, 0, regs));
}
</font>
</pre>
</tt>
Architectures which don't will implement them as loops which call
ptrace(PTRACE_GETREG, ...) or ptrace(PTRACE_SETREG, ...) for each register.
<p>
<b>semaphore.c</b>
<p>
This implements the architecture's semaphore primitives. It is highly
recommended to steal this from the underlying architecture by having
the Makefile make a link from arch/$(SUBARCH)/kernel/semaphore.c to
arch/um/sys-$(SUBARCH)/semaphore.c.
<p>
<b>checksum.c or checksum.S</b>
<p>
This implements the architecture's ip checksumming. This is
stolen from the underlying architecture in the same manner as
semaphore.c.
<p>
<b>sigcontext.c</b>
<p>
This defines fill_in_sigcontext, which copies data from a
struct_pt_regs into a struct sigcontext. The sigcontext will be
whatever was declared by UM_ALLOCATE_SC.
<p>
<b>Other files</b>
<p>
If Linux on your architecture defines any private system calls, you
will need to implement them here. Normally, you can take the code
from the underlying architecture, and you might get away with linking
to the files in the other architecture that implement them.
</blockquote>
<a name="include/asm-um/*.h"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">include/asm-um/*.h</font>
</b>
</td>
</tr>
</table>
<blockquote head="include/asm-um/*.h">
After implementing the stuff described above, you will build your
shiny new UML, and the make will blow up horribly. The reason is that
almost all of the headers in include/asm-um simply include their
counterparts from include/asm-$(SUBARCH), like this:
<tt>
<pre>
<font size="-1">
#ifndef __UM_POLL_H
#define __UM_POLL_H
#include "asm/arch/poll.h"
#endif
</font>
</pre>
</tt>
Some of these headers will define things in ways that are incompatible
with their use in UML and some will need different definitions on
different architectures.
<p>
This has been handled in two ways. Where there are definitions that
differ from architecture to architecture or where simple modifications
need to be made to a header, those arch-dependent definitions are put in
include/asm/archparam-$(SUBARCH).h, which is linked to by
include/asm/archparam.h. archparam.h, in turn, is included by the
headers which need those changes. At this point, they are
<ul>
<li>
elf.h - has a number of differences between i386 and ppc
</li>
<li>
delay.h - i386 and ppc declare a delay value differently
</li>
<li>
hw_irq.h - ppc defines hard_irq_{enter,exit}, but not
irq_{enter,exit}, so ppc defines irq_{enter,exit} as hard_irq_{enter,exit}
</li>
<li>
string.h - ppc blows up unless __HAVE_ARCH_STRRCHR is defined, which
is done here
</li>
</ul>
When archparam.h isn't sufficient, then the header is replaced by a
symlink which points to an architecture-specific header. This header
includes the generic UML header, but is able to put whatever it wants
around that include, which enables it to do things like hide
definitions and wrap ifdefs around the header. This has been done to
system.h, processor.h, and sigcontext.h.
<p>
As an example, on ppc, include/asm-um/processor.h is now a symbolic
link to include/asm-um/processor-ppc.h. processor-ppc.h wraps an
ifdef around the UML generic processor.h and turns on a cpp symbol:
<tt>
<pre>
<font size="-1">
#ifndef __UM_PROCESSOR_PPC_H
#define __UM_PROCESSOR_PPC_H
#if defined(__ASSEMBLY__)
#define CONFIG_ALL_PPC
#include "asm/processor-generic.h"
#endif
#endif
</font>
</pre>
</tt>
</blockquote>
<a name="Debugging the new port"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">Debugging the new port</font>
</b>
</td>
</tr>
</table>
<blockquote head="Debugging the new port">
There's no algorithm for doing this stage of the port, so I'll just
describe a number of useful tricks.
<p>
gdb is available. Use it. It's usable for any part of the kernel
after the beginning of start_kernel. If you need to debug anything
before that, the 'debugtrace' option is handle. It causes the tracing
thread to stop and wait to be attached with gdb. Then you can step
through the very early boot before start_kernel.
<p>
If you're post-mortem-ing a bug and you want to see what just happened
inside UML, there are some arrays which store some useful recent
history:
<ul>
<li>
signal_record - stores the last 1024 signals seen by the tracing
thread, including the host pid of the process getting the signal, the
time, and the IP at which the signal happened. This is a circular
buffer and the latest entry is at index signal_index - 1.
</li>
<li>
syscall_record - the same, except it stores process system calls. It
stores the system call number, the return value (and 0xdeadbeef is
stored there if it hasn't returned), the UML pid of the process, and
the time. It is indexed by syscall_index, so the most recent entry is
at index syscall_index - 1.
</li>
</ul>
These provide a decent picture of what UML has been doing lately.
Looking for unusual things here immediately before a bug happened is a
useful debugging technique. Correlating timestamps between the two
arrays is also sometimes useful.
<p>
If you have reproducable memory corruption, an extremely useful way to
track it down is to set the page that it happens on read-only and to
see what seg faults when it tries writing to that page. Obviously,
this only works if there aren't legitimate writes happening to that
page at the same time.
</blockquote>
</td>
</tr>
</table>
<center>
<font size="-1">
Hosted at </font>
<a href="http://sourceforge.net">
<IMG alt="SourceForge Logo" border="0" height="31" width="88" src="http://sourceforge.net/sflogo.php?group_id=429">
</a>
</center>
</body>
</html>
|