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 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
|
<!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 src="http://user-mode-linux.sourceforge.net/uml-small.png" height="171" width="120"/>
</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://uml.harlowhill.com">The UML Wiki</a></font>
<br>
<font size="-1"><a href="http://usermodelinux.org">UML Community Site</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/roadmap.html">The UML roadmap</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/skas.html">Skas Mode</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/patches.html">Incremental Patches</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/tests.html">Test Suite</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/devanon.html">Host memory use</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/resize.html">Resizing 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/honeypots.html">UML Honeypots</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>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/2G-2G.html">UML on 2G/2G hosts</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/lksct/index.html">Adding a UML system call</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/nesting.html">Running nested UMLs</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>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/wvu2002/wvu2002.htm">WVU 2002 (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/ists_rt/ists_rt.htm">Security Roundtable (html)</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/ols2002/ols2002.html">OLS 2002 slides</a></font>
<br>
<font size="-1"><a href="http://user-mode-linux.sourceforge.net/slides/lwe2005/LWE2005.html">LWE 2005 slides</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/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="include/asm-um"/><table width="100%" bgcolor="#e0e0e0">
<tr>
<td>
<b>
<font color="black">include/asm-um</font>
</b>
</td>
</tr>
</table>
<blockquote head="include/asm-um">
Every architecture needs to provide a set of headers to the generic
kernel. These are located in include/asm-$(ARCH). UML mostly uses
the headers of the underlying architecture. It does this by creating
a symlink from include/asm-um/arch to include/asm-$(SUBARCH). Most of
UML's headers then just include "asm/arch/header.h". As an example,
this is rwlock.h
<tt>
<pre>
<font size="-1">
#ifndef __UM_RWLOCK_H
#define __UM_RWLOCK_H
#include "asm/arch/rwlock.h"
#endif
</font>
</pre>
</tt>
Almost all of UML's headers look exactly like that. Some of the rest
are architecture-independent headers private to UML. You don't need to
worry about these.
<p>
Some headers include the underlying arch headers, but also need to
change them in some way. For example, the UML ptrace-generic.h includes
asm/arch/ptrace.h because it needs some definitions from there.
However, there are things it doesn't want because it needs to define
its own, such as struct pt_regs. So, the underlying architecture's
struct pt_regs is renamed by doing the following
<tt>
<pre>
<font size="-1">
#define pt_regs pt_regs_subarch
#include "asm/arch/ptrace.h"
#undef pt_regs
</font>
</pre>
</tt>
This changes the name of the underlying architecture's pt_regs to
struct pt_regs_subarch, allowing UML to define its own struct
pt_regs. This practice of taking most of the contents other
architecture's header and defining the unwanted bits away is useful,
but it also causes problems which porter have to deal with. For
example, the ppc ptrace.h includes "asm/system.h", which includes the
UML system.h. Since the UML system.h contains references to pt_regs
and it's being included by a header that has had pt_regs renamed to
pt_regs_subarch, the UML system.h references are similarly renamed.
This causes conflicts against files which expect references to
pt_regs. There have been several attempts to update UML/ppc and this
problem has stymied them. It's far from an insolvable problem, but it
involves staring at confusing sequences of includes, figuring out
what's happening, and how to fix it.
<p>
There are also a few headers which are archtecture-dependent.
<ul>
<li>
archparam-$(SUBARCH).h
<br>
This is a header for miscellaneous architecture-dependent
definitions. In a lot of cases, a mostly-generic header can get its
non-generic definitions from a separate header. In these cases, the
definitions are put in archparam-$(SUBARCH).h and that header includes
asm/archparam.h, which is a symbolic link to archparam-$(SUBARCH).h.
<p>
The i386 archparam mostly includes definitions for elf.h, such as
platform-specific register initializatins. The ppc archparam is
similar, adding a couple of definitions for hardirq.h and a couple of
other headers.
</li>
<li>
processor-$(SUBARCH).h
<br>
This header exists because UML/ppc needs a little logic to choose
between including one header and another.
</li>
<li>
ptrace-$(SUBARCH).h
<br>
This defines architecture-dependent access macros into struct
pt_regs. These macros aren't used in generic code, but you may need
some when you define ELF_PLAT_INIT and a few other things. Those
definitions should go here.
</li>
<li>
sigcontext-$(SUBARCH).h
<br>
This is similar to processor-$(SUBARCH).h. It exists so that that the
occurrences of pt_regs in asm-$(SUBARCH)/sigcontext.h can be defined
out of the way.
</li>
<li>
system-$(SUBARCH).h
<br>
This header exists so that UML/ppc can define the ppc _switch_to out
of the way. The i386 version doesn't do anything.
</li>
<li>
ptrace-generic.h
<br>
ptrace-generic.h isn't architecture-dependent, but it does put some
requirements on arch/um/include/sysdep/ptrace.h, which you will meet
below. It defines its struct pt_regs as
<tt>
<pre>
<font size="-1">
struct pt_regs {
struct uml_pt_regs regs;
};
</font>
</pre>
</tt>
and it's up to the architecture to define struct uml_pt_regs. This is
done this way because some UML userspace code needs to refer to
register values. So, uml_pt_regs is the structure that is safe for
userspace code to look at, which pt_regs is for kernel code. Access
macros for pt_regs therefore just call the equivalent access macro for
uml_pt_regs, like
<tt>
<pre>
<font size="-1">
#define PT_REGS_IP(r) UPT_IP(&(r)->regs)
</font>
</pre>
</tt>
and these should be defined in arch/um/include/sysdep/ptrace.h, which
should be able to be included in both user and kernel code. This
means it can't include either libc headers or kernel headers, but may
include safe UML headers.
</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>frame.h</b>
<p>
One of the first things that UML does when it boots is it creates and
saves a signal frame. This will be used when it delivers signals to
its processes. It will be copied onto the process stack and the data
in the original frame, like the signal number, sigcontext contents,
and restorer will be replaced. UML knows how to replace this stuff
because it figured out where in the stack frame it is. And it did
that by having the signal handler record the address of the signal,
the sigcontext structure, and a few other things.
<p>
This is done in a mostly architecture-independent way, but a little
help is needed from architecture-specific code. That code goes into
frame.h.
<p>
What's needed here are definitions of:
<tt>
<pre>
<font size="-1">
static inline unsigned long frame_restorer(void)
</font>
</pre>
</tt>
This returns the location on the stack of the signal state restorer.
On i386, this is the return address, which is next to the frame
pointer, so the i386 definition is
<tt>
<pre>
<font size="-1">
static inline unsigned long frame_restorer(void)
{
unsigned long *fp;
fp = __builtin_frame_address(0);
return((unsigned long) (fp + 1));
}
</font>
</pre>
</tt>
<tt>
<pre>
<font size="-1">
static inline unsigned long frame_sp(void)
</font>
</pre>
</tt>
This returns the value of the stack pointer when the signal handler is
first entered. Note that this is not necessarily the same value as
when the signal handler code is executing because it may have been
adjusted for local variables.
<p>
On i386, this is four bytes more than the frame pointer, so its
definition is
<tt>
<pre>
<font size="-1">
static inline unsigned long frame_sp(void)
{
unsigned long *fp;
fp = __builtin_frame_address(0);
return((unsigned long) (fp + 1));
}
</font>
</pre>
</tt>
In addition, there may be a need for the architecture to save more
information from the signal frame. There are two pairs of structures
and procedures which allow you to do this. The first pair are
expected to record raw addresses from the frame:
<tt>
<pre>
<font size="-1">
struct arch_frame_data_raw {
...
};
static inline void setup_arch_frame_raw(struct arch_frame_data_raw *data,
struct sigcontext *sc)
</font>
</pre>
</tt>
The arch_frame_data_raw may contain anything you want and
setup_arch_frame_raw is expected to fill it in. Both may be empty.
<p>
i386 needs to know the size of the floating point state that's on the
signal frame, so these save the address of the beginning of the sigcontext
structure, where the floating point state ends:
<tt>
<pre>
<font size="-1">
struct arch_frame_data_raw {
unsigned long sc_end;
};
static inline void setup_arch_frame_raw(struct arch_frame_data_raw *data,
struct sigcontext *sc)
{
data->sc_end = (unsigned long) sc;
data->sc_end += sizeof(*sc);
}
</font>
</pre>
</tt>
Then a similar structure and function pair is used to process the raw
addresses into something that's usable later. The i386 code assumes
that the floating point state runs from the top of the stack (which is
alone on its own page, so the top of the stack is the end of the page)
to the start of the sigcontext structure:
<tt>
<pre>
<font size="-1">
struct arch_frame_data {
int fpstate_size;
};
static inline void setup_arch_frame(struct arch_frame_data_raw *in,
struct arch_frame_data *out)
{
unsigned long fpstate_start = in->sc_end;
fpstate_start &= ~PAGE_MASK;
out->fpstate_size = PAGE_SIZE - fpstate_start;
}
</font>
</pre>
</tt>
<p>
<b>ptrace.h</b>
<p>
ptrace.h deals with the machine's register set. It defines the
following:
<tt>
<pre>
<font size="-1">
struct uml_pt_regs
</font>
</pre>
</tt>
which should contain a system call number, a set of system call
arguments, a flag saying whether the kernel was entered from userspace
or kernelspace, and a pointer to the sigcontext structure on the
current stack. The i386 definition is
<tt>
<pre>
<font size="-1">
struct uml_pt_regs {
unsigned long args[6];
long syscall;
int is_user;
void *sc;
};
</font>
</pre>
</tt>
which may in fact be architecture-independent. The only thing that
may need changing is the size of args[].
<tt>
<pre>
<font size="-1">
EMPTY_UML_PT_REGS
</font>
</pre>
</tt>
which is an initializer for uml_pt_regs. The i386 definition is
<tt>
<pre>
<font size="-1">
#define EMPTY_UML_PT_REGS { \
syscall : -1, \
args : { [0 ... 5] = 0 }, \
is_user : 0, \
sc : NULL }
</font>
</pre>
</tt>
<tt>
<pre>
<font size="-1">
UPT_REG(regs, reg)
</font>
</pre>
</tt>
which returns the value of the appropriate register from the saved
sigcontext.
<tt>
<pre>
<font size="-1">
UPT_SET(regs, reg, val)
</font>
</pre>
</tt>
which sets the value of the appropriate register in the saved
sigcontext to whatever value is passed in. The i386 definitions of
these are big switch statements
<tt>
<pre>
<font size="-1">
#define UPT_REG(regs, reg) \
({ unsigned long val; \
switch(reg){ \
case EIP: val = UPT_IP(regs); break; \
case UESP: val = UPT_SP(regs); break; \
case EAX: val = UPT_EAX(regs); break; \
case EBX: val = UPT_EBX(regs); break; \
case ECX: val = UPT_ECX(regs); break; \
case EDX: val = UPT_EDX(regs); break; \
case ESI: val = UPT_ESI(regs); break; \
case EDI: val = UPT_EDI(regs); break; \
case EBP: val = UPT_EBP(regs); break; \
case ORIG_EAX: val = UPT_ORIG_EAX(regs); break; \
case CS: val = UPT_CS(regs); break; \
case SS: val = UPT_SS(regs); break; \
case DS: val = UPT_DS(regs); break; \
case ES: val = UPT_ES(regs); break; \
case FS: val = UPT_FS(regs); break; \
case GS: val = UPT_GS(regs); break; \
case EFL: val = UPT_EFLAGS(regs); break; \
default : \
panic("Bad register in UPT_REG : %d\n", reg); \
val = -1; \
} \
val; \
})
#define UPT_SET(regs, reg, val) \
do { \
switch(reg){ \
case EIP: UPT_IP(regs) = val; break; \
case UESP: UPT_SP(regs) = val; break; \
case EAX: UPT_EAX(regs) = val; break; \
case EBX: UPT_EBX(regs) = val; break; \
case ECX: UPT_ECX(regs) = val; break; \
case EDX: UPT_EDX(regs) = val; break; \
case ESI: UPT_ESI(regs) = val; break; \
case EDI: UPT_EDI(regs) = val; break; \
case EBP: UPT_EBP(regs) = val; break; \
case ORIG_EAX: UPT_ORIG_EAX(regs) = val; break; \
case CS: UPT_CS(regs) = val; break; \
case SS: UPT_SS(regs) = val; break; \
case DS: UPT_DS(regs) = val; break; \
case ES: UPT_ES(regs) = val; break; \
case FS: UPT_FS(regs) = val; break; \
case GS: UPT_GS(regs) = val; break; \
case EFL: UPT_EFLAGS(regs) = val; break; \
default : \
panic("Bad register in UPT_SET : %d\n", reg); \
break; \
} \
} while (0)
</font>
</pre>
</tt>
In addition to whatever macros you call from any additional PT_REGS_*
macros you define, there are a few that you definitely need
equivalents to. These will generally call into sigcontext macros
since they need to modify the current sigcontext.
<ul>
<li>
UPT_SET_SYSCALL_RETURN
<br>
This sets the system call return value
</li>
<li>
UPT_RESTART_SYSCALL
<br>
This does whatever is necessary to make sure that the current system
call will restart when userspace is entered. Backing up the IP so
that it points at the system call instruction is probably enough.
</li>
<li>
UPT_ORIG_SYSCALL
<br>
This is the original location of the system call number. i386 moves
it from EAX to ORIG_EAX, so it refers to EAX. This is used when
restarting a system call to restore the registers to their original
values.
</li>
<li>
UPT_SYSCALL_NR
<br>
This pulls the system call number from the uml_pt_regs. On i386, it
comes directly from the uml_pt_regs structure. On other
architectures, it may make sense to get it from the sigcontext.
</li>
<li>
UPT_SYSCALL_RET
<br>
This returns the system call return value.
</li>
</ul>
<p>
<b>ptrace_user.h</b>
<p>
This file defines a set of access macros into the hosts's pt_regs
structure. This is purely a userspace file which is used by parts of
UML which use ptrace to pull the process registers from the host
kernel and need to interpret them. These are the definitions that
should be here, and they should be implemented in terms of register
definitions in the host <asm/ptrace.h>.
<ul>
<li>
PT_SYSCALL_NR
</li>
<li>
PT_SYSCALL_NR_OFFSET
<br>
The pt_regs index and ptrace offset of the system call number
</li>
<li>
PT_SYSCALL_ARG1_OFFSET
</li>
<li>
PT_SYSCALL_ARG2_OFFSET
</li>
<li>
PT_SYSCALL_ARG3_OFFSET
</li>
<li>
PT_SYSCALL_ARG4_OFFSET
</li>
<li>
PT_SYSCALL_ARG5_OFFSET
<br>
The offsets of the system call arguments
</li>
<li>
PT_SYSCALL_RET_OFFSET
<br>
The offset of the system call return value
</li>
<li>
PT_IP
</li>
<li>
PT_IP_OFFSET
<br>
The pt_regs index and offset of the IP
</li>
<li>
PT_SP
<br>
The index of the stack pointer
</li>
<li>
FRAME_SIZE
</li>
<li>
FRAME_SIZE_OFFSET
<br>
If the host pt_regs doesn't define FRAME_SIZE, set it to the number of
general purpose registers. Set FRAME_SIZE_OFFSET to the maximum
offset for PTRACE_GETREGS.
</li>
<li>
FP_FRAME_SIZE
</li>
<li>
FPX_FRAME_SIZE
<br>
These are the number of floating point registers and extended floating
point registers, respectively. The second is likely x86-specific. If
you don't define UM_HAVE_GETFPREGS/UM_HAVE_SETFPREGS or
UM_HAVE_GETFPXREGS/UM_HAVE_SETFPXREGS (see below), you can leave the
corresponding _FRAME_SIZE undefined.
</li>
<li>
UM_HAVE_GETREGS
</li>
<li>
UM_HAVE_SETREGS
</li>
<li>
UM_HAVE_GETFPREGS
</li>
<li>
UM_HAVE_SETFPREGS
</li>
<li>
UM_HAVE_GETFPXREGS
</li>
<li>
UM_HAVE_SETFPXREGS
<br>
These should be defined if the architecture defines PTRACE_GETREGS,
PTRACE_SETREGS, PTRACE_GETFPREGS, PTRACE_SETFPREGS, PTRACE_GETFPXREGS,
PTRACE_,SETFPXREGS respectively.
</li>
</ul>
<p>
<b>sigcontext.h</b>
<p>
sigcontext.h defines a few sigcontext-related macros. Some of them
are accessed through the PT_REGS and UPT_REGS macros, so their
details are up to you. You will have to define similar things at the
very least, so here's what i386 defines
<ul>
<li>
SC_RESTART_SYSCALL
<br>
Does whatever IP fiddling is needed to cause the current system call
to restart when userspace is re-entered. On i386, this just subtracts
2 from the IP since a system call instruction is 2 bytes long.
</li>
<li>
SC_SET_SYSCALL_RETURN
<br>
Sets the system call return value. On i386, this just sets %eax. On
ppc, it does a little more than that.
</li>
<li>
SC_FAULT_ADDR
</li>
<li>
SC_FAULT_WRITE
<br>
These two are called from generic UML code, so you have to implement
these as described. On a segfault, they pick out from a sigcontext
the fault address and whether the fault was on a write.
</li>
<li>
SEGV_IS_FIXABLE
<br>
This evaluates to non-zero if a segfault is one that can be fixed by
mapping in a page or changing page protections. If not, then it
returns zero, and the faulting process will simply be segfaulted.
This is called from generic UML code.
</li>
<li>
SC_START_SYSCALL
<br>
This is a general hook that's called at the start of a system call
before ptrace gets to see it. On x86, strace expects %eax to contain
-ENOSYS when it sees a system call entry. So, this macro sets that.
On other architectures, this may do nothing. This is called from
generic UML code.
</li>
<li>
void sc_to_regs(struct uml_pt_regs *regs, struct sigcontext *sc,
unsigned long syscall)
<br>
This is called at the beginning of a system call to fill in a pt_regs
structure from the sigcontext. It parses the system call from the
sigcontext and fills in the system call number and the arguments in
the pt_regs. This is called from generic UML code.
</li>
</ul>
<p>
<p>
<b>syscalls.h</b>
<p>
syscalls.h defines any architecture-specific system calls. It does so
by defining
<ul>
<li>
ARCH_SYSCALLS
<br> which is a set of array element initializers which will be
included in the initialization of the system call table. The i386
ARCH_SYSCALLS looks in part like this
<tt>
<pre>
<font size="-1">
#define ARCH_SYSCALLS \
[ __NR_mmap ] = old_mmap_i386, \
[ __NR_select ] = old_select, \
[ __NR_vm86old ] = sys_ni_syscall, \
[ __NR_modify_ldt ] = sys_modify_ldt, \
...
[ 222 ] = sys_ni_syscall,
</font>
</pre>
</tt>
</li>
</ul>
Also, you must define
<ul>
<li>
LAST_ARCH_SYSCALL
<br>
to be the last initialized element defined by ARCH_SYSCALLS.
This is used to fill the end of the system call table properly.
</li>
</ul>
In addition, syscalls.h defines
<ul>
<li>
EXECUTE_SYSCALL(syscall, regs)
<br>
which calls into a system call entry. The i386 pt_regs has been
arranged so that it can just be dumped on the stack and the right
thing will happen
<tt>
<pre>
<font size="-1">
#define EXECUTE_SYSCALL(syscall, regs) (*sys_call_table[syscall])(*regs);
</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 a few sigcontext-related functions
<ul>
<li>
int sc_size(void *data)
<br>
How big is a sigcontext? On x86, this takes the floating point state
into account as well as just the sigcontext structure itself.
</li>
<li>
int copy_sc_to_user(void *to_ptr, void *from_ptr, void *data)
<br>
Copies a sigcontext to a process stack. It must use copy_to_user, not
memcopy.
</li>
<li>
int copy_sc_from_user(void *to_ptr, void *from_ptr, void *data)
<br>
Copies a sigcontext from a process stack into kernel memory.
Similarly, this must use copy_from_user, not memcopy.
</li>
<li>
void sc_to_sc(void *to_ptr, void *from_ptr)
<br>
This copies a sigcontext from one kernel stack to another. It is used
during thread creation (kernel_thread(), fork(), or clone()) to
initialize a kernel stack with a signal frame that the new process can
return from.
</li>
</ul>
<p>
<b>sysrq.c</b>
<p>
This needs to define
<ul>
<li>
void show_regs(struct pt_regs *regs)
<br>
For the benefit of the SysRq driver.
</li>
</ul>
<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="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 src="http://sourceforge.net/sflogo.php?group_id=429" width="88" height="31" border="0" alt="SourceForge Logo">
</a>
</center>
</body>
</html>
|