Skip to content

Commit f9caddf

Browse files
committed
Lots of additions to syscall section (esp for clone) and instructions added
1 parent 1704216 commit f9caddf

File tree

3 files changed

+112
-7
lines changed

3 files changed

+112
-7
lines changed

asm_slides/system_calls_5.rst

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ Safe memory access
553553

554554
* Think in terms of "transactions"
555555
* The lock prefix
556-
* Some special properties of xchg
557-
* Introduction cmpxchg
556+
* Some instructions, such as xchg, implicitly lock
558557

559558
----
560559

@@ -583,6 +582,65 @@ Clone
583582

584583
----
585584

585+
Flags
586+
=====
587+
588+
Some flags we'll want for our thread library:
589+
590+
.. code:: c
591+
592+
#define CLONE_VM 0x00000100 /* Set if VM shared between processes. */
593+
#define CLONE_FS 0x00000200 /* Set if fs info shared between processes. */
594+
#define CLONE_FILES 0x00000400 /* Set if open files shared between processes. */
595+
#define CLONE_SIGHAND 0x00000800 /* Set if signal handlers shared. */
596+
#define CLONE_THREAD 0x00010000 /* Set to add to same thread group. */
597+
598+
----
599+
600+
Basic Steps to Success
601+
======================
602+
603+
1.) Allocate Stack Space
604+
2.) Call Clone
605+
3.) Transfer Control to Intended function
606+
607+
----
608+
609+
Allocating Stack Space
610+
======================
611+
612+
* The stack grows down, and thus we need to give the high part of the new stack segment to clone
613+
* mmap is the best choice to do this, as it has flags that let us specify that we wish to use the allocated memory as a thread stack:
614+
615+
.. code:: c
616+
617+
#define MAP_GROWSDOWN 0x0100
618+
619+
----
620+
621+
Calling Clone
622+
=============
623+
624+
* As seen above, clone has several arguments
625+
* For our purposes, only two are really useful: the flags argument (RDI), and a pointer to our new stack
626+
* We'll need to get to the end of the new stack:
627+
628+
.. code:: nasm
629+
630+
lea rcx, [rcx + STACK_SIZE] ; assuming rcx contains a pointer to our newly allocated stack segment
631+
632+
----
633+
634+
Running the Thread Function
635+
===========================
636+
637+
----
638+
639+
Join
640+
====
641+
642+
----
643+
586644
Making Atomic adds and Comparisons
587645
==================================
588646

@@ -601,9 +659,15 @@ Creating a Simple Spinlock
601659

602660
.. code:: nasm
603661
604-
lock bts
605-
; ...
606-
lock btr
662+
lock_func:
663+
; ...
664+
lock bts
665+
; ...
666+
667+
unlock_func:
668+
; ...
669+
lock btr
670+
; ...
607671
608672
----
609673

handouts/lab_instructions.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#####################################################################
2+
#
3+
# Using the labs
4+
#
5+
#####################################################################
6+
7+
1.) Required Software
8+
2.) Building and Running
9+
10+
---------------------------------------------------------------------
11+
12+
1.) List of Required Software
13+
14+
* CMake, version >= 3.2
15+
* NASM
16+
* A C(++) compiler, such as clang or gcc
17+
* GDB
18+
* Visual Studio 2015 (Windows labs), and NASM for Windows
19+
20+
2.) Building and Running
21+
22+
CMake Projects
23+
--------------
24+
25+
The bulk of the labs are all set up to use CMake to build/run,
26+
and should just require the following sequence of commands:
27+
28+
cd project_dir
29+
cmake .
30+
cmake --build .
31+
./labX
32+
33+
Windows Projects
34+
----------------
35+
36+
The Windows-specific labs are all bundled with a VS2015 solution file,
37+
which expects to find NASM in the solution directory (This will likely be
38+
ported to CMake in the future). The project should be buildable either via
39+
Visual Studio, or by running the following command:
40+
41+
"%VS140COMNTOOLS%vsvars32.bat" && msbuild <solution file>

labs/Lab12/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44
#include <stdint.h>
5-
#include <fcntl.h>
5+
#include <sched.h>
66

77
extern "C" size_t first_func(int*, int*, int*);
88
extern "C" size_t second_func(int*, int*, int*);
@@ -60,6 +60,6 @@ int main(int argc, char** argv)
6060
first_test();
6161
second_test();
6262
third_test();
63-
S_IRUSR
63+
CLONE_FILES
6464
return 0;
6565
}

0 commit comments

Comments
 (0)