-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargument_passing.html
953 lines (470 loc) · 25.8 KB
/
argument_passing.html
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
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Argument Passing · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="user_memory.html" />
<link rel="prev" href="introduction.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="header">Introduction</li>
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../introduction/getting_started.html">
<a href="../introduction/getting_started.html">
Getting Started
</a>
</li>
<li class="chapter " data-level="1.3" data-path="../introduction/grading.html">
<a href="../introduction/grading.html">
Grading
</a>
</li>
<li class="chapter " data-level="1.4" data-path="../introduction/legal_and_ethical_issues.html">
<a href="../introduction/legal_and_ethical_issues.html">
Legal and Ethical Issues
</a>
</li>
<li class="header">Project1: Threads</li>
<li class="chapter " data-level="2.1" data-path="../project1/introduction.html">
<a href="../project1/introduction.html">
Introduction
</a>
</li>
<li class="chapter " data-level="2.2" data-path="../project1/alarm_clock.html">
<a href="../project1/alarm_clock.html">
Alarm Clock
</a>
</li>
<li class="chapter " data-level="2.3" data-path="../project1/priority_scheduling.html">
<a href="../project1/priority_scheduling.html">
Priority Scheduling
</a>
</li>
<li class="chapter " data-level="2.4" data-path="../project1/advanced_scheduler.html">
<a href="../project1/advanced_scheduler.html">
Advanced Scheduler
</a>
</li>
<li class="chapter " data-level="2.5" data-path="../project1/FAQ.html">
<a href="../project1/FAQ.html">
FAQ
</a>
</li>
<li class="header">Project2: User Programs</li>
<li class="chapter " data-level="3.1" data-path="introduction.html">
<a href="introduction.html">
Introduction
</a>
</li>
<li class="chapter active" data-level="3.2" data-path="argument_passing.html">
<a href="argument_passing.html">
Argument Passing
</a>
</li>
<li class="chapter " data-level="3.3" data-path="user_memory.html">
<a href="user_memory.html">
User Memory
</a>
</li>
<li class="chapter " data-level="3.4" data-path="system_call.html">
<a href="system_call.html">
System Calls
</a>
</li>
<li class="chapter " data-level="3.5" data-path="process_termination.html">
<a href="process_termination.html">
Process Termination Messages
</a>
</li>
<li class="chapter " data-level="3.6" data-path="deny_write.html">
<a href="deny_write.html">
Denying Writes to Executables
</a>
</li>
<li class="chapter " data-level="3.7" data-path="dup.html">
<a href="dup.html">
Extend File Descriptor (Extra)
</a>
</li>
<li class="chapter " data-level="3.8" data-path="FAQ.html">
<a href="FAQ.html">
FAQ
</a>
</li>
<li class="header">Project3: Virtual Memory</li>
<li class="chapter " data-level="4.1" data-path="../project3/introduction.html">
<a href="../project3/introduction.html">
Introduction
</a>
</li>
<li class="chapter " data-level="4.2" data-path="../project3/vm_management.html">
<a href="../project3/vm_management.html">
Memory Management
</a>
</li>
<li class="chapter " data-level="4.3" data-path="../project3/anon.html">
<a href="../project3/anon.html">
Anonymous Page
</a>
</li>
<li class="chapter " data-level="4.4" data-path="../project3/stack_growth.html">
<a href="../project3/stack_growth.html">
Stack Growth
</a>
</li>
<li class="chapter " data-level="4.5" data-path="../project3/memory_mapped_files.html">
<a href="../project3/memory_mapped_files.html">
Memory Mapped Files
</a>
</li>
<li class="chapter " data-level="4.6" data-path="../project3/swapping.html">
<a href="../project3/swapping.html">
Swap In/Out
</a>
</li>
<li class="chapter " data-level="4.7" data-path="../project3/cow.html">
<a href="../project3/cow.html">
Copy-on-Write (Extra)
</a>
</li>
<li class="chapter " data-level="4.8" data-path="../project3/FAQ.html">
<a href="../project3/FAQ.html">
FAQ
</a>
</li>
<li class="header">Project4: File System</li>
<li class="chapter " data-level="5.1" data-path="../project4/introduction.html">
<a href="../project4/introduction.html">
Introduction
</a>
</li>
<li class="chapter " data-level="5.2" data-path="../project4/indexed_and_extensible_files.html">
<a href="../project4/indexed_and_extensible_files.html">
Indexed and Extensible Files
</a>
</li>
<li class="chapter " data-level="5.3" data-path="../project4/subdirectories.html">
<a href="../project4/subdirectories.html">
Subdirectories and Soft Links
</a>
</li>
<li class="chapter " data-level="5.4" data-path="../project4/buffer_cache.html">
<a href="../project4/buffer_cache.html">
Buffer Cache
</a>
</li>
<li class="chapter " data-level="5.5" data-path="../project4/synchronization.html">
<a href="../project4/synchronization.html">
Synchronization
</a>
</li>
<li class="chapter " data-level="5.6" data-path="../project4/mount.html">
<a href="../project4/mount.html">
Mount (Extra)
</a>
</li>
<li class="chapter " data-level="5.7" data-path="../project4/journaling.html">
<a href="../project4/journaling.html">
Filesystem Journaling (Extra)
</a>
</li>
<li class="chapter " data-level="5.8" data-path="../project4/FAQ.html">
<a href="../project4/FAQ.html">
FAQ
</a>
</li>
<li class="header">Appendix</li>
<li class="chapter " data-level="6.1" data-path="../appendix/threads.html">
<a href="../appendix/threads.html">
Threads
</a>
</li>
<li class="chapter " data-level="6.2" data-path="../appendix/synchronization.html">
<a href="../appendix/synchronization.html">
Synchronization
</a>
</li>
<li class="chapter " data-level="6.3" data-path="../appendix/memory_allocation.html">
<a href="../appendix/memory_allocation.html">
Memory Allocation
</a>
</li>
<li class="chapter " data-level="6.4" data-path="../appendix/virtual_address.html">
<a href="../appendix/virtual_address.html">
Virtual Address
</a>
</li>
<li class="chapter " data-level="6.5" data-path="../appendix/page_table.html">
<a href="../appendix/page_table.html">
Page Table
</a>
</li>
<li class="chapter " data-level="6.6" data-path="../appendix/debugging_tools.html">
<a href="../appendix/debugging_tools.html">
Debugging Tools
</a>
</li>
<li class="chapter " data-level="6.7" data-path="../appendix/development_tools.html">
<a href="../appendix/development_tools.html">
Development Tools
</a>
</li>
<li class="chapter " data-level="6.8" data-path="../appendix/hash_table.html">
<a href="../appendix/hash_table.html">
Hash Table
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >Argument Passing</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h3 id="argument-passing">Argument Passing</h3>
<p><strong>Setup the argument for user program in <code>process_exec()</code></strong></p>
<h4 id="x86-64-calling-convention">x86-64 Calling Convention</h4>
<p>This section summarizes important points of the convention used for normal
function calls on 64-bit x86-64 implementations of Unix. Some details are
omitted for brevity. For more detail, you can refer
<a href="https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI" target="_blank">System V AMD64 ABI</a>.</p>
<p>The calling convention works like this:</p>
<ol>
<li>User-level applications use as integer registers for passing the sequence
<code>%rdi</code>, <code>%rsi</code>, <code>%rdx</code>, <code>%rcx</code>, <code>%r8</code> and <code>%r9</code>.</li>
<li>The caller pushes the address of its next instruction (the return address) on
the stack and jumps to the first instruction of the callee. A single x86-64
instruction, <code>CALL</code>, does both.</li>
<li>The callee executes.</li>
<li>If the callee has a return value, it stores it into register RAX.</li>
<li>The callee returns by popping the return address from the stack and jumping
to the location it specifies, using the x86-64 <code>RET</code> instruction.</li>
</ol>
<p>Consider a function <code>f()</code> that takes three int arguments. This diagram shows a
sample stack frame and register state as seen by the callee at the beginning of
step 3 above, supposing that <code>f()</code> is invoked as <code>f(1, 2, 3)</code>.
The initial stack address is arbitrary:</p>
<pre><code> +----------------+
stack pointer --> 0x4747fe70 | return address |
+----------------+
RDI: 0x0000000000000001 | RSI: 0x0000000000000002 | RDX: 0x0000000000000003
</code></pre><h4 id="program-startup-details">Program Startup Details</h4>
<p>The Pintos C library for user programs designates <code>_start()</code>, in
<code>lib/user/entry.c</code>, as the entry point for user programs. This function is a
wrapper around <code>main()</code> that calls <code>exit()</code> if <code>main()</code> returns:</p>
<pre><code class="lang-C"><span class="hljs-keyword">void</span>
_start (<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[]) {
<span class="hljs-built_in">exit</span> (main (argc, argv));
}
</code></pre>
<p>The kernel must put the arguments for the initial function on the register
before it allows the user program to begin executing. The arguments are passed
in the same way as the normal calling convention.</p>
<p>Consider how to handle arguments for the following example command:
<code>/bin/ls -l foo bar</code>.</p>
<ol>
<li><p>Break the command into words: <code>/bin/ls</code>, <code>-l</code>, <code>foo</code>, <code>bar</code>.</p>
</li>
<li><p>Place the words at the top of the stack. Order doesn't matter,
because they will be referenced through pointers.</p>
</li>
<li><p>Push the address of each string plus a null pointer sentinel, on the
stack, in right-to-left order. These are the elements of argv. The null pointer
sentinel ensures that <code>argv[argc]</code> is a null pointer, as required by the C
standard. The order ensures that <code>argv[0]</code> is at the lowest virtual address.
Word-aligned accesses are faster than unaligned accesses, so for best
performance round the stack pointer down to a multiple of 8 before the first
push.</p>
</li>
<li><p>Point <code>%rsi</code> to <code>argv</code> (the address of <code>argv[0]</code>) and set <code>%rdi</code> to <code>argc</code>.</p>
</li>
<li><p>Finally, push a fake "return address": although the entry function will never
return, its stack frame must have the same structure as any other.</p>
</li>
</ol>
<p>The table below shows the state of the stack and the relevant registers right
before the beginning of the user program.
Note that the stack grows down.</p>
<table>
<thead>
<tr>
<th>Address</th>
<th>Name</th>
<th>Data</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>0x4747fffc</td>
<td>argv[3][...]</td>
<td>'bar\0'</td>
<td>char[4]</td>
</tr>
<tr>
<td>0x4747fff8</td>
<td>argv[2][...]</td>
<td>'foo\0'</td>
<td>char[4]</td>
</tr>
<tr>
<td>0x4747fff5</td>
<td>argv[1][...]</td>
<td>'-l\0'</td>
<td>char[3]</td>
</tr>
<tr>
<td>0x4747ffed</td>
<td>argv[0][...]</td>
<td>'/bin/ls\0'</td>
<td>char[8]</td>
</tr>
<tr>
<td>0x4747ffe8</td>
<td>word-align</td>
<td>0</td>
<td>uint8_t[]</td>
</tr>
<tr>
<td>0x4747ffe0</td>
<td>argv[4]</td>
<td>0</td>
<td>char *</td>
</tr>
<tr>
<td>0x4747ffd8</td>
<td>argv[3]</td>
<td>0x4747fffc</td>
<td>char *</td>
</tr>
<tr>
<td>0x4747ffd0</td>
<td>argv[2]</td>
<td>0x4747fff8</td>
<td>char *</td>
</tr>
<tr>
<td>0x4747ffc8</td>
<td>argv[1]</td>
<td>0x4747fff5</td>
<td>char *</td>
</tr>
<tr>
<td>0x4747ffc0</td>
<td>argv[0]</td>
<td>0x4747ffed</td>
<td>char *</td>
</tr>
<tr>
<td>0x4747ffb8</td>
<td>return address</td>
<td>0</td>
<td>void (*) ()</td>
</tr>
</tbody>
</table>
<blockquote>
<p>RDI: 4 | RSI: 0x4747ffc0</p>
</blockquote>
<p>In this example, the stack pointer would be initialized to <code>0x4747ffb8</code>
As shown above, your code should start the stack at the <code>USER_STACK</code>, which is
defined in <code>include/threads/vaddr.h</code>.</p>
<p>You may find the non-standard <code>hex_dump()</code> function, declared in <code><stdio.h></code>,
useful for debugging your argument passing code.</p>
<h4 id="implement-the-argument-passing">Implement the argument passing.</h4>
<p>Currently, <code>process_exec()</code> does not support passing arguments to new
processes. Implement this functionality, by extending <code>process_exec()</code> so
that instead of simply taking a program file name as its argument, it divides it
into words at spaces. The first word is the program name, the second word is the
first argument, and so on. That is, <code>process_exec("grep foo bar")</code> should run
grep passing two arguments foo and bar.</p>
<p>Within a command line, multiple spaces are equivalent to a single space, so that
<code>process_exec("grep foo bar")</code> is equivalent to our original example. You can
impose a reasonable limit on the length of the command line arguments. For
example, you could limit the arguments to those that will fit in a single page
(4 kB). (There is an unrelated limit of 128 bytes on command-line arguments that
the pintos utility can pass to the kernel.)</p>
<p>You can parse argument strings any way you like. If you're lost, look at
<code>strtok_r()</code>, prototyped in <code>include/lib/string.h</code> and implemented with thorough
comments in <code>lib/string.c</code>. You can find more about it by looking at the man
page (run <code>man strtok_r</code> at the prompt).</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="introduction.html" class="navigation navigation-prev " aria-label="Previous page: Introduction">
<i class="fa fa-angle-left"></i>
</a>
<a href="user_memory.html" class="navigation navigation-next " aria-label="Next page: User Memory">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Argument Passing","level":"3.2","depth":1,"next":{"title":"User Memory","level":"3.3","depth":1,"path":"project2/user_memory.md","ref":"project2/user_memory.md","articles":[]},"previous":{"title":"Introduction","level":"3.1","depth":1,"path":"project2/introduction.md","ref":"project2/introduction.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"project2/argument_passing.md","mtime":"2021-04-20T02:49:31.552Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-20T03:04:38.373Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>