├── .DS_Store
├── LICENSE
├── README.md
├── Theory HW
├── .DS_Store
├── README.md
├── homework 1
│ ├── .DS_Store
│ ├── Feb26.md
│ └── hw1(1).pdf
├── homework 2
│ ├── .DS_Store
│ ├── Mar13.md
│ ├── gantt.png
│ └── hw2.pdf
├── homework 3
│ ├── .DS_Store
│ ├── OS homework week 6.md
│ └── hw3.docx
├── homework 4
│ ├── .DS_Store
│ ├── Apr 8.md
│ └── hw4.docx
├── homework 5
│ ├── .DS_Store
│ ├── May 7.md
│ ├── hw5.docx
│ └── 第4题注释.png
├── homework 6
│ ├── .DS_Store
│ ├── Image 2020-6-1 at 7.32 PM.jpg
│ ├── OS homework 6.md
│ └── hw6.docx
├── homework 7
│ ├── .DS_Store
│ ├── OS homework week 16.md
│ └── hw7.docx
└── pics
│ ├── .DS_Store
│ ├── image-20200313210549010.png
│ ├── image-20200313210600306.png
│ ├── image-20200313211941836.png
│ ├── image-20200313212707825.png
│ ├── image-20200314151524219.png
│ ├── pro1-RAID-5.png
│ ├── pro1-RAID-6.png
│ ├── process_hw3.png
│ ├── process_thread_hw3.png
│ └── thread_hw3.png
├── lab_1
├── .DS_Store
├── Lab 01 summary.md
├── commit.png
├── lab1.pdf
└── 实验一.md
├── lab_2
├── .DS_Store
├── lab2.pdf
├── lab_2_original_file.md
├── references
│ ├── .DS_Store
│ └── lab2_shell.c
├── release_HW
│ ├── Operating System Lab 2 Report.md
│ ├── Operating System Lab 2 Report.pdf
│ ├── include
│ │ ├── a.out.h
│ │ ├── asm
│ │ │ ├── io.h
│ │ │ ├── memory.h
│ │ │ ├── segment.h
│ │ │ └── system.h
│ │ ├── const.h
│ │ ├── ctype.h
│ │ ├── errno.h
│ │ ├── fcntl.h
│ │ ├── linux
│ │ │ ├── config.h
│ │ │ ├── fdreg.h
│ │ │ ├── fs.h
│ │ │ ├── hdreg.h
│ │ │ ├── head.h
│ │ │ ├── kernel.h
│ │ │ ├── mm.h
│ │ │ ├── sched.h
│ │ │ ├── sys.h
│ │ │ └── tty.h
│ │ ├── signal.h
│ │ ├── stdarg.h
│ │ ├── stddef.h
│ │ ├── string.h
│ │ ├── sys
│ │ │ ├── stat.h
│ │ │ ├── times.h
│ │ │ ├── types.h
│ │ │ ├── utsname.h
│ │ │ └── wait.h
│ │ ├── termios.h
│ │ ├── time.h
│ │ ├── unistd.h
│ │ └── utime.h
│ ├── kernel
│ │ ├── Makefile
│ │ ├── asm.o
│ │ ├── asm.s
│ │ ├── blk_drv
│ │ │ ├── Makefile
│ │ │ ├── blk.h
│ │ │ ├── blk_drv.a
│ │ │ ├── floppy.c
│ │ │ ├── floppy.o
│ │ │ ├── hd.c
│ │ │ ├── hd.o
│ │ │ ├── ll_rw_blk.c
│ │ │ ├── ll_rw_blk.o
│ │ │ ├── ramdisk.c
│ │ │ └── ramdisk.o
│ │ ├── chr_drv
│ │ │ ├── Makefile
│ │ │ ├── chr_drv.a
│ │ │ ├── console.c
│ │ │ ├── console.o
│ │ │ ├── kb.S
│ │ │ ├── keyboard.o
│ │ │ ├── keyboard.s
│ │ │ ├── rs_io.o
│ │ │ ├── rs_io.s
│ │ │ ├── serial.c
│ │ │ ├── serial.o
│ │ │ ├── tty_io.c
│ │ │ ├── tty_io.o
│ │ │ ├── tty_ioctl.c
│ │ │ └── tty_ioctl.o
│ │ ├── exit.c
│ │ ├── exit.o
│ │ ├── fork.c
│ │ ├── fork.o
│ │ ├── kernel.o
│ │ ├── math
│ │ │ ├── Makefile
│ │ │ ├── math.a
│ │ │ ├── math_emulate.c
│ │ │ └── math_emulate.o
│ │ ├── mktime.c
│ │ ├── mktime.o
│ │ ├── panic.c
│ │ ├── panic.o
│ │ ├── print_str2num.c
│ │ ├── print_str2num.o
│ │ ├── printk.c
│ │ ├── printk.o
│ │ ├── sched.c
│ │ ├── sched.o
│ │ ├── signal.c
│ │ ├── signal.o
│ │ ├── sys.c
│ │ ├── sys.o
│ │ ├── system_call.o
│ │ ├── system_call.s
│ │ ├── traps.c
│ │ ├── traps.o
│ │ ├── vsprintf.c
│ │ └── vsprintf.o
│ ├── lab2_shell
│ ├── lab2_shell.c
│ ├── test.c
│ └── unistd.h
└── summary.md
├── lab_3
├── .DS_Store
├── PB18000227_艾语晨_lab3.pdf
├── Summary for lab 3.md
├── Summary.md
├── lab3.pdf
├── lab3_malloc
│ ├── Makefile
│ ├── README
│ ├── a.out
│ ├── config.h
│ ├── ep_mm.c
│ ├── ep_mm.h
│ ├── ep_mmdriver.c
│ ├── ep_mmdriver.h
│ ├── memlib.c
│ ├── memlib.h
│ ├── mm.c
│ ├── mm.h
│ ├── mmdriver.c
│ ├── mmdriver.h
│ └── traces
│ │ ├── 1.rep
│ │ └── 2.rep
├── lab_3_original_file.md
└── pics
│ ├── .DS_Store
│ ├── 1C64ED04F0583A572319C8BF19BEF507.png
│ ├── 4C902BB2D48E2EA2CF0B7491A9536E6F.png
│ ├── 68B39952D24FD2D6BDE44CB22719F731.jpg
│ ├── 7E56877638AF7D85EE3AC7CD4DF41BE7.png
│ └── FAFB6D6FBAD2A01122A1F38442F5F2E4.png
└── lab_4
├── .DS_Store
├── OS lab 4 report.md
├── OS lab 4 report.pdf
├── OS lab 4 summary.md
├── lab4-file-system.pdf
└── pics
├── .DS_Store
└── 4_1_1.png
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/.DS_Store
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Lapland Stark
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Operating-System
2 | ---
3 | This is a repository for the OS course (prof.YongKun Lee) in USTC.
4 |
5 | > Note :
6 | The pics that cannot display are placed in the pics folder
7 |
--------------------------------------------------------------------------------
/Theory HW/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/README.md:
--------------------------------------------------------------------------------
1 | # OS homework
2 |
3 | Since GitHub do not support MathJax grammar, therefore the math formulae are not able to be displayed normally. Please clone the repo to local folder or download the markdown files and open it with Typora.
4 |
--------------------------------------------------------------------------------
/Theory HW/homework 1/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 1/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 1/Feb26.md:
--------------------------------------------------------------------------------
1 | # 操作系统作业1
2 |
3 | ---
4 |
PB18000227 艾语晨
5 |
6 | ## 1. 从系统的角度理解,操作系统主要负责哪两大功能
7 |
8 | > answer
9 |
10 | - OS is a control program
11 | - OS is a resource allocator
12 | - 即:控制程序和资源分配者
13 |
14 | ## 2. 什么是系统调用?阐述系统调用与API的区别和逻辑关系
15 |
16 | > answer
17 |
18 | - *Definition*: System calls are the programming interface between processes and the OS kernel
19 | - 系统调用是在进程与操作系统内核之间的接口
20 | - *Differences*: API 是应用程序接口,跟kernel没有直接关系,而系统调用是通过中断向内核发请求,实现内核提供的某些服务
21 | - *Relationship*: Mostly a system call is accessed by programs via a high-level API rather than direct system call use
22 | - 大多数情况下系统调用是通过一个高层次的API实现,而不是直接做系统调用
23 |
24 | ## 3. 阐述Dual Mode的工作机制,以及采用Dual Mode的原因
25 |
26 | > answer
27 | >
28 | > > 工作机制
29 |
30 | - 在进程执行时,分为 User mode 和 Kernel mode,由一个 mode bit 区分。
31 | - Some instructions designed as privileged, only executable in kernel mode
32 | - System call changes mode to kernal, return from call that resets it to user
33 | - 总体上来说,在执行一些必须在 kernel 里面执行的命令时,进入 kernel mode,执行完毕后返回 user mode
34 | >> 原因
35 |
36 | Dual-mode operation allows OS to protect itself and other system components .
37 |
38 | Dual mode 可以更好的保护操作系统,因为在执行一些有危险错误(如非法访问并尝试修改越界数据,修改操作系统只读文件等)或恶意程序时,由于需要进入 kernel mode 而需要额外的权限,故可以避免一部分错误的发生
39 |
40 | ## 4. 分析Monolithic结构,层次化结构,模块化结构和微内核结构的优劣
41 |
42 | > answer
43 |
44 | ||Monolithic|Layered|MicroKernel|Modules|
45 | |-|-|-|-|-|
46 | |merits|更好的性能(效率)|易于建构和 debug;将 DS(数据结构?)、Ops(操作)、硬件对于上层结构隐藏起来|易于扩展:新的 services 加到用户空间,不需要修改内核;易于将操作系统移植到新的结构;更可靠安全(在 kernel 里面跑的代码更少)|类似于分层结构,但更灵活;也类似于微内核结构,但效率更高|
47 | |weaknesses|难于实现和维护|不好定义不同层次; 效率问题|性能取决于用户空间到内核空间的传输效率,用户空间的性能开销导致性能开销比较大|需要保证设计使操作系统各模块之间的耦合度低,且无法找到一个可靠的模块开发程序,增加新的内容时可能产生系统碎片化问题和安全问题|
48 |
49 | ## 5. 举例说明采用机制与策略分离的设计原因
50 |
51 | > answer
52 |
53 | - Timer mechanism 计时器 (for CPU protection)
54 | Policy decision: How long the timer is to be set?
55 | Mechanism 和 Policy 分开的原因是在具体使用时,或者不同版本,要根据使用环境、要求、不同需求的优先级的不同来改变设计,这时只需改变 Policy策略,就可以,而不需要修改 mechanism
56 |
--------------------------------------------------------------------------------
/Theory HW/homework 1/hw1(1).pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 1/hw1(1).pdf
--------------------------------------------------------------------------------
/Theory HW/homework 2/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 2/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 2/Mar13.md:
--------------------------------------------------------------------------------
1 | # OS homework for week 4
2 |
3 | by 艾语晨 PB18000227
4 |
5 | ---
6 |
7 | ### Problem 1
8 |
9 | > Q : Including the initial parent process, how many processes are created by the program shown in Figure 1?
10 |
11 | 
12 |
13 |
14 |
15 | > A :
16 |
17 | 16 (Including the initial process of this program)
18 |
19 | ### Problem 2
20 |
21 | > Q : Explain the circumstances under which the line of code marked printf (‘‘LINE J’’) in Figure 2 will be reached.
22 |
23 |
24 |
25 | >A :
26 |
27 | When the `execlp()` function call is failed to be called, it will not reset the process but continue to execute the printf func, with a return value of `-1`.
28 |
29 | ### Problem 3
30 |
31 | > Q : Using the program in Figure 3, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)
32 |
33 |
34 |
35 | > A :
36 |
37 | The PID values are as the table below :
38 |
39 | | position | value |
40 | | :------: | :---: |
41 | | A | 0 |
42 | | B | 2603 |
43 | | C | 2603 |
44 | | D | 2600 |
45 |
46 | ### Problem 4
47 |
48 | > Q : Using the program shown in Figure 4, explain what the output will be at lines X and Y.
49 |
50 |
51 |
52 | > A :
53 |
54 | ```powershell
55 | CHILD: 0 CHILD: -1 CHILD: -4 CHILD: -9 CHILD: -16 # LINE X
56 | PARENT: 0 PARENT: 1 PARENT: 2 PARENT: 3 PARENT: 4 # LINE Y
57 | ```
58 |
59 | > Note : Cause this program does *not* have a line break at its output...So the results were put altogether, yet it seems that there *is* a blank space between ':' and '%'.
60 |
61 | ### Problem 5
62 |
63 | > Q : For the program in Figure 5, will LINE X be executed, and explain why.
64 |
65 | ```c
66 | int main(void) {
67 | printf("before execl ...\n");
68 | execl("/bin/ls", "/bin/ls", NULL);
69 | printf("after execl ...\n"); /*LINE: X*/
70 | return 0;
71 | }
72 | ```
73 |
74 | > A :
75 |
76 | LINE X will *not* be actually executed (except for the condition that the function call `execl` is not executed since an error has occurred).
77 |
78 | The reason is that the system call of `exec()` replaces the current process image with a new process image. While the new one is there to execute, the original process code or statistics are covered and will not be executed.
79 |
80 | ### Problem 6
81 |
82 | > Q : Explain why “terminated state” is necessary for processes.
83 |
84 | > A :
85 |
86 | In a `fork(), exec(), wait()` , a parent process is blocked when using a `wait()` call, and only can wake up again only when receiving a SIGCHLD *from an `exit()` call made by the dying child process*, destroying the zombie process, and move on to its former execution.
87 |
88 | **Terminated state is a mechanism that child process give feedback information to its parent. It can be used for waking up parent process and notifying parent process of the termination information of its child.**
89 |
90 | When the child process is terminated, the kernel notifies the parent of the child process about the termination of its child.
91 |
92 | So, if there isn't a "terminated state", the parent will never be waken up, and the zombie will never be eliminated.
93 |
94 | ### Problem 7
95 |
96 | > Q : Explain what a zombie process is and when a zombie process will be eliminated (i.e., its PCB entry is removed from kernel).
97 |
98 | > A :
99 |
100 | When a process ends its execution and invokes `exit()`, it changes into terminated state, keeping its storage in the kernel-space memory to a minimum. It's called a *zombie process* before it's given a clean death by its parent's `wait()` call.
101 |
102 | A zombie process is eliminated either when its SIGCHLD is picked up by its parent process's signal handling routine, or become an orphan and be adopted by process 'init', before being destroyed by the periodically called `wait()`.
--------------------------------------------------------------------------------
/Theory HW/homework 2/gantt.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 2/gantt.png
--------------------------------------------------------------------------------
/Theory HW/homework 2/hw2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 2/hw2.pdf
--------------------------------------------------------------------------------
/Theory HW/homework 3/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 3/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 3/hw3.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 3/hw3.docx
--------------------------------------------------------------------------------
/Theory HW/homework 4/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 4/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 4/hw4.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 4/hw4.docx
--------------------------------------------------------------------------------
/Theory HW/homework 5/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 5/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 5/hw5.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 5/hw5.docx
--------------------------------------------------------------------------------
/Theory HW/homework 5/第4题注释.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 5/第4题注释.png
--------------------------------------------------------------------------------
/Theory HW/homework 6/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 6/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 6/Image 2020-6-1 at 7.32 PM.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 6/Image 2020-6-1 at 7.32 PM.jpg
--------------------------------------------------------------------------------
/Theory HW/homework 6/OS homework 6.md:
--------------------------------------------------------------------------------
1 | # OS homework 6
2 |
3 | PB18000227 艾语晨
4 |
5 | ## Problem 1
6 |
7 | > Q :
8 | >
9 | > Consider a RAID organization comprising five disks in total, how many blocks are accessed in order to perform the following operations for RAID-5 and RAID-6?
10 | >
11 | > a. An update of one block of data
12 | >
13 | > b. An update of seven continuous blocks of data. Assume that the seven contiguous blocks begin at a boundary of a stripe.
14 |
15 | > A :
16 |
17 | Read &Write are *both* access operations, RRW **do not** need to read parity block(s).
18 |
19 | **a. An update of one block of data**
20 |
21 | - RAID-5 : 2 blocks (The target block (R&W) and the parity block (R&W)) (RMW)
22 | - RAID-6 : 3 blocks (The target block (R&W) and the parity blocks (R&W)) (RMW)
23 |
24 | **b. An update of seven continuous blocks of data**
25 |
26 | - RAID-5 : 10 blocks (Read B4 and write A1-A4, Ap, B1-B3, Bp) (RRW)
27 | - RAID-6 : 16 blocks (Read Cp, Cq, C1and write A1-A3, Ap-Aq, B1-B3, Bp-Bq, C1, Cp-Cq) (Row 1,2: RRW; Row 3: RMW)
28 |
29 | > Note: the problem said **blocks** instead of **times**, therefore read and write to one block is calculated as one time
30 |
31 | Data is stored in form of this: (I smeared unchanged blocks)
32 |
33 | 
34 |
35 | 
36 |
37 | ## Problem 2
38 |
39 | > Q :
40 | >
41 | > Explain what open-file table is and why we need it
42 |
43 | > A :
44 |
45 | - OS keeps a table containing information about all open files (per- process and system-wide table). When a file is closed, its attributes in the table would be deleted.
46 |
47 | - The reason is that we can save the time of searching the directory for locating the file (I/O operation).
48 |
49 | ## Problem 3
50 |
51 | > Q :
52 | >
53 | > Explain the concept of file and directory, and what does “755” mean for file permission
54 |
55 | > A :
56 |
57 | - A *file* is a uniform logical view of stored information provided by OS.
58 | - **OS perspective**: A file is a logical storage unit (a sequence of logical records), it is an abstract data type
59 | - **User perspective**: the smallest allotment of logical secondary storage
60 | - A *directory* is a file
61 |
62 | - A directory file records all the files including directories that are belonging to it
63 |
64 | - '755' equals to '111 101 101' in binary. So it means :
65 |
66 | | To whom | Readable? | Writeable? | Executable? |
67 | | --------------- | --------- | ---------- | ----------- |
68 | | To owner | True | True | True |
69 | | To file's group | True | False | True |
70 | | To others | True | False | True |
71 |
72 | ## Problem 4
73 |
74 | > Q :
75 | >
76 | > Explain the problems of using continuous allocation for file system layout and how to solve them
77 |
78 | > A :
79 |
80 | - Problems
81 |
82 | 1. **External Fragmentation**
83 | - We have enough space, but there is no holes that I can satisfy the request
84 | - If we move files to fill those holes (defragmentation process), that would be a really expensive cost
85 | 2. **The Growth Problem**
86 | - If a file need to grow, we might have to move the files after it to have more space...
87 |
88 | - Solutions
89 | - Trial 2.0 : borrow idea from linked list, chop the storage device into equal-sized blocks
90 |
91 | ## Problem 5
92 |
93 | > Q :
94 | >
95 | > What are the advantages of the variation of linked allocation that uses a FAT to chain together the blocks of a file? What is the major problem of FAT
96 |
97 | > A :
98 |
99 | - Advantages :
100 | - If the FAT is presented as **an array**, and a cached version of FAT is kept inside the kernel, the random access problem can be eased
101 | - The FAT is stored in memory, therefore it can be fast to search the FAT
102 | - A tradeoff can be made between (memory) space and performance by not storing the entire set of block locations in memory, so that not so much memory would be used
103 | - Major problem :
104 | - The FAT eats too much memory to be stored
105 |
106 | ## Problem 6
107 |
108 | > Q :
109 | >
110 | > Consider a file system similar to the one used by UNIX with indexed allocation, and assume that every file uses only one block. How many disk I/O operations might be required to read the contents of a small local file at /a/b/c in the following two cases? Should provide the detailed workflow.
111 | >
112 | > a. Assume that none of the disk blocks and inodes is currently being cached.
113 | >
114 | > b. Assume that none of the disk blocks is currently being cached but all inodes are in memory
115 |
116 | > A :
117 |
118 | **Not same as FAT, indexed allocation don't have to cache the index table in memory.** So every time (when not cached) we visit a file/directory, it would costs 2 I/O operations. However, the first step of the visit (such as the 'a' in this problem) will costs 3.
119 |
120 | Thus, on 'a' condition, there are total $3+2+2=7$ times; on 'b' condition, there are total $2+1+1=4$ times
121 |
122 | ## Problem 7
123 |
124 | > Q :
125 | >
126 | > Consider a file system that uses inodes to represent files. Disk blocks are 8-KB in size and a pointer to a disk block requires 4 bytes. This file system has 12 direct disk blocks, plus single, double, and triple indirect disk blocks. What is the maximum size of a file that can be stored in this file system
127 |
128 | > A :
129 |
130 | Referring to page 58 in ch9_Part2
131 |
132 | 1 block : 8 kB = $2^{13}$ Bytes; address length : 4 bytes
133 |
134 | Total maximum size : $12\times2^{13}+2^{24}+2^{35}+2^{46}\approx7.04\times10^{13}$ bytes $\approx64$ TB
135 |
136 |
--------------------------------------------------------------------------------
/Theory HW/homework 6/hw6.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 6/hw6.docx
--------------------------------------------------------------------------------
/Theory HW/homework 7/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 7/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/homework 7/OS homework week 16.md:
--------------------------------------------------------------------------------
1 | # OS homework week 16
2 |
3 | PB18000227 艾语晨
4 |
5 | ### Question 1
6 |
7 | > Q :
8 | >
9 | > **What is the 8+3 naming convention in FAT32 file system, and how to manage long filenames?**
10 |
11 | > A :
12 |
13 | - The 8+3 naming convention means :
14 | - In the 0~10 Bytes, 8 characters (each use 1 byte) are allocated for file name, including 1 byte representing if unallocated, while 3 characters are allocated for file extension
15 | - Use LFN entries to store filename, every LFN entry is able to store 13 characters in Unicode, yet the sequence of LFN is upside down
16 |
17 | ### Question 2
18 |
19 | > Q :
20 | >
21 | > **How are director entries managed in FAT and Ext file systems**
22 |
23 | > A :
24 |
25 | - FAT : A FAT table manages all director entries, a directory entry stores *filename*, extension name, etc. In every block visited stores the address of the next block, or EOF
26 | - Ext : A directory file's content is the inode-number-table of files in this directory, while the inode table, as a layered linked list, provides the address of every block in this file
27 |
28 | ### Question 3
29 |
30 | > Q :
31 | >
32 | > **What is the difference between hard link and symbolic link**
33 |
34 | > A :
35 |
36 | A hard link is a directory entry pointing to an existing file, and it will increase the link count of the file's inode.
37 |
38 | A symbolic link is a file. Unlike the hard link, a new inode is created for each symbolic link. It stores the pathname (shortcut)
39 |
40 | ### Question 4
41 |
42 | > Q :
43 | >
44 | > **What are the initial link counts when a regular file or a directory is created? Why**
45 |
46 | > A :
47 |
48 | The initial link count of a regular file is 1, which is its pathname.
49 |
50 | The initial link count of a directory is 2, including its pathname and `.` (a hark link to itself)
51 |
52 | ### Question 5
53 |
54 | > Q :
55 | >
56 | > **What is the difference between data journaling and metadata journaling? Explain the operation sequence for each of the two journaling methods**
57 |
58 | > A :
59 |
60 | Data journaling issues all blocks at once. Its operation sequence is "Journal write (Write the contents of the transaction (including TxB, metadata, and data)) -> Journal commit (metadata, and data (including TxE)) -> Checkpoint (Write the contents of the update to their on-disk locations)"
61 |
62 | Metadata journaling logs metadata only. Its operation sequence is "Data write -> Journal metadatawrite -> Journal commit -> Checkpoint metadata -> Free", yet the first two steps can be issued in parallel.
63 |
64 | ### Question 6
65 |
66 | > Q :
67 | >
68 | > **What are the three I/O control methods**
69 |
70 | > A :
71 |
72 | Polling, Interrupts, DMA (Direct Memory Access)
73 |
74 | (以下复制粘贴自我自己的组成原理作业😂不再翻译一遍了就)
75 |
76 | 程序查询方式:由于在有I/O操作的时候,计算机就会暂停主程序,转而执行I/O,加以I/O的低效率,会浪费很多CPU周期和资源
77 |
78 | 程序中断方式:由于采用了CPU和I/O设备并行的结构,故占用很少的CPU周期(每一次I/O请求用一个时钟周期)
79 |
80 | DMA方式:数据交换不经过CPU,占用CPU不访问内存的时间,几乎不占用CPU周期
81 |
82 | ### Question 7
83 |
84 | > Q :
85 | >
86 | > **List at least three kinds of I/O devices and explain how to provide a standard and uniform application I/O interface**
87 |
88 | > A :
89 |
90 | - Devices
91 | - Disk, Keyboard, CD-ROM, socket
92 | - Provide a standard and uniform application I/O interface
93 | - Abstraction, encapsulation, layering
94 | - Abstract general models from various I/O devices, while put the interface into kernel, modifying interface ports towards devices.
95 |
96 | ### Question 8
97 |
98 | > Q :
99 | >
100 | > **What services are provided by the kernel I/O subsystem**
101 |
102 | > A :
103 |
104 | - **I/O scheduling**
105 | - Maintain a per-device queue
106 | - Re-ordering the requests
107 | - Average waiting time, fairness, etc.
108 | - **Buffering** - store data in memory while transferring between devices
109 | - To cope with device speed mismatch
110 | - To cope with device transfer size mismatch
111 | - To maintain “copy semantics” (e.g., copy from application’s buffer to kernel buffer)
112 | - **Caching** - faster device holding copy of data
113 | - Always just a copy
114 | - Key to performance
115 | - Sometimes combined with buffering
116 | - **Spooling** - hold output for a device
117 | - If device can serve only one request at a time, e.g., Printing
118 | - **Error handling** and **I/O protection**
119 | - OS can recover from disk read error, device unavailable, transient write failures
120 | - All I/O instructions defined to be privileged
121 | - **Power management, etc.**
122 |
123 | 填空,名词解释,简答题,综合题(算法&计算)
124 |
125 |
--------------------------------------------------------------------------------
/Theory HW/homework 7/hw7.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/homework 7/hw7.docx
--------------------------------------------------------------------------------
/Theory HW/pics/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/.DS_Store
--------------------------------------------------------------------------------
/Theory HW/pics/image-20200313210549010.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/image-20200313210549010.png
--------------------------------------------------------------------------------
/Theory HW/pics/image-20200313210600306.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/image-20200313210600306.png
--------------------------------------------------------------------------------
/Theory HW/pics/image-20200313211941836.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/image-20200313211941836.png
--------------------------------------------------------------------------------
/Theory HW/pics/image-20200313212707825.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/image-20200313212707825.png
--------------------------------------------------------------------------------
/Theory HW/pics/image-20200314151524219.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/image-20200314151524219.png
--------------------------------------------------------------------------------
/Theory HW/pics/pro1-RAID-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/pro1-RAID-5.png
--------------------------------------------------------------------------------
/Theory HW/pics/pro1-RAID-6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/pro1-RAID-6.png
--------------------------------------------------------------------------------
/Theory HW/pics/process_hw3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/process_hw3.png
--------------------------------------------------------------------------------
/Theory HW/pics/process_thread_hw3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/process_thread_hw3.png
--------------------------------------------------------------------------------
/Theory HW/pics/thread_hw3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/Theory HW/pics/thread_hw3.png
--------------------------------------------------------------------------------
/lab_1/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_1/.DS_Store
--------------------------------------------------------------------------------
/lab_1/Lab 01 summary.md:
--------------------------------------------------------------------------------
1 | # Lab 01 report
2 |
3 | ---
4 | by Lapland Stark
5 |
6 | > This only includes some of the contents of this lab.
7 |
8 | ---
9 | ## Task One
10 |
11 | ### A choice
12 | use `echo` to write into files, or vim/nano/code into it...
13 |
14 | ```shell
15 | / # echo >
16 | ```
17 |
18 | >Caution:
19 |
20 | run the following command under the Linux-0.11 folder (your lab workspace)
21 |
22 | ```shell
23 | $ make start
24 | ```
25 |
26 | ### problem you might face with
27 | #### in the qemu
28 |
29 | If you try to do the command
30 | ```shell
31 | / # mv 1.txt dir1
32 | ```
33 | you might face the following error: `mv: 1.txt: Not owner`
34 |
35 | ## Task Three
36 |
37 | >caution:
38 |
39 | In the part "卸载文件系统hdc",you must check carefully about the actual name of **your own** hdc. In the tutorial, the first column of the 'hdc' is /dev/loop15, but it can be different on your Ubuntu.
40 |
41 | ### problem you might face with
42 |
43 | #### in the qemu (Linux-0.11)
44 |
45 | During "查看Linux0.11文件", when you are executing this command:
46 | ```shell
47 | / # more hello.txt
48 | ```
49 | Remind that you must be in the `/usr` path
50 |
51 | #### in the Ubuntu system
52 |
53 | You must create a 'hdc' dir first, then do the `mount` command.
54 |
55 | 有一个奇怪的···在之前 `umount` 的 dir 下挂载新的文件系统,会报错(俺也不知道为啥)
56 | ```shell
57 | mount: /home/stark/oslab/Linux-0.11/hdc: failed to setup loop device for /home/stark/oslab/Linux-0.11/hdc-0.11.img.
58 | ```
59 | >疑似解决方案 (经验,不一定可行)
60 | 1. 删除 hdc 文件夹 以及挂载的文件系统里面所有更改(添加 )的文件(夹)
61 | 2. 重启 Ubuntu
62 | 3. 多试几次
63 | 4. df -h(这个我最不理解)
64 |
65 | #### While unmounting a file system
66 |
67 | The old files might remain there, so make sure you've deleted them before you redo this experiment.
68 |
69 | ## While committing
70 |
71 | Remember that on MacOS, there's NO embedded FTP file installed, so use a Ubuntu (not xUbuntu) to commit your homework. The server is in the 'files' app, like the screenshot attached in this folder.
72 |
--------------------------------------------------------------------------------
/lab_1/commit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_1/commit.png
--------------------------------------------------------------------------------
/lab_1/lab1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_1/lab1.pdf
--------------------------------------------------------------------------------
/lab_2/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/.DS_Store
--------------------------------------------------------------------------------
/lab_2/lab2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/lab2.pdf
--------------------------------------------------------------------------------
/lab_2/references/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/references/.DS_Store
--------------------------------------------------------------------------------
/lab_2/references/lab2_shell.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | #define MAX_CMDLINE_LENGTH 256 /*max cmdline length in a line*/
8 | #define MAX_CMD_LENGTH 16 /*max single cmdline length*/
9 | #define MAX_CMD_NUM 16 /*max single cmdline length*/
10 |
11 | #ifndef NR_TASKS /*max task num*/
12 | #define NR_TASKS 64
13 | #endif
14 |
15 | #define SHELL "/bin/sh"
16 |
17 | static int *child_pid = NULL; /*save running children's pid*/
18 |
19 | /* popen,输入为命令和类型("r""w"),输出执行命令进程的I/O文件描述符 */
20 | int os_popen(const char* cmd, const char type){
21 | int i, pipe_fd[2], proc_fd;
22 | pid_t pid;
23 |
24 | if (type != 'r' && type != 'w') {
25 | printf("popen() flag error\n");
26 | return NULL;
27 | }
28 |
29 | if(child_pid == NULL) {
30 | if ((child_pid = calloc(NR_TASKS, sizeof(int))) == NULL)
31 | return NULL;
32 | }
33 |
34 | if (pipe(pipe_fd) < 0) {
35 | printf("popen() pipe create error\n");
36 | return NULL;
37 | }
38 |
39 | /* 1. 使用系统调用创建新进程 */
40 |
41 |
42 | /* 2. 子进程部分 */
43 | {
44 | if (type == 'r') {
45 | /* 2.1 关闭pipe无用的一端,将I/O输出发送到父进程 */
46 | close(pipe_fd[]);
47 | if (pipe_fd[] != STDOUT_FILENO) {
48 | dup2(pipe_fd[], STDOUT_FILENO);
49 | close(pipe_fd[]);
50 | }
51 | } else {
52 | /* 2.2 关闭pipe无用的一端,接收父进程提供的I/O输入 */
53 | close(pipe_fd[]);
54 | if (pipe_fd[] != STDIN_FILENO) {
55 | dup2(pipe_fd[], STDIN_FILENO);
56 | close(pipe_fd[]);
57 | }
58 | }
59 | /* 关闭所有未关闭的子进程文件描述符(无需修改) */
60 | for (i=0;i0)
62 | close(i);
63 | /* 2.3 通过exec系统调用运行命令 */
64 | execl();
65 | /* 也可使用execlp execvp等 */
66 | _exit(127);
67 | }
68 | /* 3. 父进程部分 */
69 | if (type == 'r') {
70 | close(pipe_fd[]);
71 | proc_fd = pipe_fd[];
72 | } else {
73 | close(pipe_fd[]);
74 | proc_fd = pipe_fd[];
75 | }
76 | child_pid[proc_fd] = pid;
77 | return proc_fd;
78 | }
79 |
80 | /* 关闭正在打开的管道,等待对应子进程运行结束(无需修改) */
81 | int os_pclose(const int fno) {
82 | int stat;
83 | pid_t pid;
84 | if (child_pid == NULL)
85 | return -1;
86 | if ((pid = child_pid[fno]) == 0)
87 | return -1;
88 | child_pid[fno] = 0;
89 | close(fno);
90 | while (waitpid(pid, &stat, 0)<0)
91 | if(errno != EINTR)
92 | return -1;
93 | return stat;
94 | }
95 |
96 | int os_system(const char* cmdstring) {
97 | pid_t pid;
98 | int stat;
99 |
100 | if(cmdstring == NULL) {
101 | printf("nothing to do\n");
102 | return 1;
103 | }
104 |
105 | /* 4.1 创建一个新进程 */
106 |
107 |
108 | /* 4.2 子进程部分 */
109 | {
110 |
111 | }
112 |
113 | /* 4.3 父进程部分: 等待子进程运行结束 */
114 | {
115 |
116 | }
117 |
118 | return stat;
119 | }
120 |
121 | /* 对cmdline按照";"做划分,返回划分段数 */
122 | int parseCmd(char* cmdline, char cmds[MAX_CMD_NUM][MAX_CMD_LENGTH]) {
123 | int i,j;
124 | int offset = 0;
125 | int cmd_num = 0;
126 | char tmp[MAX_CMD_LENGTH];
127 | int len = NULL;
128 | char *end = strchr(cmdline, ';');
129 | char *start = cmdline;
130 | while (end != NULL) {
131 | memcpy(cmds[cmd_num], start, end - start);
132 | cmds[cmd_num++][end - start] = '\0';
133 |
134 | start = end + 1;
135 | end = strchr(start, ';');
136 | };
137 | len = strlen(cmdline);
138 | if (start < cmdline + len) {
139 | memcpy(cmds[cmd_num], start, (cmdline + len) - start);
140 | cmds[cmd_num++][(cmdline + len) - start] = '\0';
141 | }
142 | return cmd_num;
143 | }
144 |
145 | void zeroBuff(char* buff, int size) {
146 | int i;
147 | for(i=0;i", 11);
161 | gets(cmdline);
162 | cmd_num = parseCmd(cmdline, cmds);
163 | for(i=0;iby PB18000227 艾语晨
4 |
5 | ### 添加Linux系统调用
6 |
7 | #### 大概描述实验过程
8 |
9 | 增加系统调用需要修改相关文件,和具体的函数实现
10 |
11 | 就是按照实验文档所说的,先看一下后面的背景知识,并借助上一次习题课的讲解,最终完成
12 |
13 | #### 展示实验结果
14 |
15 | 
16 |
17 | #### 实验代码(改变部分)
18 |
19 | ##### `unistd.h`,左边的是include文件夹下,右边的是文件系统里面
20 |
21 | 
22 |
23 | ##### `sys.h`
24 |
25 | 
26 |
27 | ##### `system_call.s`
28 |
29 | 
30 |
31 | ##### `Makefile`
32 |
33 | 
34 |
35 | 
36 |
37 | ##### `print_str2num.c`
38 |
39 | 
40 |
41 | #### 回答问题
42 |
43 | - 简要描述如何在Linux-0.11添加一个系统调用
44 |
45 | > Answer :
46 |
47 | 1. 修改**system_call.s**中的系统调用数量
48 | 2. 在**sys.h**、**unistd.h**中增加相应的声明、函数指针表内容
49 | 3. 在kernel目录下实现相应的函数,并修改makefile
50 |
51 | - 系统是如何通过系统调用号索引到具体的调用函数的
52 |
53 | > Answer :
54 |
55 | 在**unistd.h**中有系统调用编号,`_syscallx`函数根据这个编号到**sys.h**中的函数指针表找到相应函数原型,然后在kernel目录下找到具体的实现
56 |
57 | - 在Linux-0.11中,系统最多支持几个参数?有什么方法可以超过这个限制吗?
58 |
59 | > Answer :
60 |
61 | 最多支持3个参数。因为在**unistd.h**里面只定义到了`_syscall3()`。超过限制的方式可以是通过修改**unistd.h**文件,定义新的`syscallx()`函数
62 |
63 | ### 熟悉Linux下常见的系统调用函数
64 |
65 | #### 大概描述代码添加过程、思路
66 |
67 | ##### `os_open()`
68 |
69 | 用于pipe的开启工作,原理如下图(取自李老师PPT,ch5part1;Page37)
70 |
71 | 
72 |
73 | 简单来说思路是生成匿名管道(第一次close关闭没有用的管道一端),重定向STDOUT/STDIN,然后解除管道原有的端口,而PARENT一端就相对简单,只是正常写/读数据即可
74 |
75 | ##### `os_system`
76 |
77 | 通过`strtok`函数进行字符串分割,来分解指令字符串,在执行各命令
78 |
79 | 
80 |
81 | ##### `main`
82 |
83 | 分需要pipe和不需要两种
84 |
85 | 需要pipe:
86 |
87 | 1. cmd1->buf:开启管道,通过read函数写进缓冲区
88 | 2. buf->cmd2:开启管道,通过write函数从缓冲区写出
89 |
90 | 不需要pipe:直接调用os_system函数即可
91 |
92 | #### 展示实验结果
93 |
94 | 
95 |
96 | ### P.S.
97 |
98 | 关于实验视频的说明:第一题的视频是录了两个然后合并起来的,但是把回答问题的部分放到前面了
--------------------------------------------------------------------------------
/lab_2/release_HW/Operating System Lab 2 Report.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/Operating System Lab 2 Report.pdf
--------------------------------------------------------------------------------
/lab_2/release_HW/include/a.out.h:
--------------------------------------------------------------------------------
1 | #ifndef _A_OUT_H
2 | #define _A_OUT_H
3 |
4 | #define __GNU_EXEC_MACROS__
5 |
6 | struct exec {
7 | unsigned long a_magic; /* Use macros N_MAGIC, etc for access */
8 | unsigned a_text; /* length of text, in bytes */
9 | unsigned a_data; /* length of data, in bytes */
10 | unsigned a_bss; /* length of uninitialized data area for file, in bytes */
11 | unsigned a_syms; /* length of symbol table data in file, in bytes */
12 | unsigned a_entry; /* start address */
13 | unsigned a_trsize; /* length of relocation info for text, in bytes */
14 | unsigned a_drsize; /* length of relocation info for data, in bytes */
15 | };
16 |
17 | #ifndef N_MAGIC
18 | #define N_MAGIC(exec) ((exec).a_magic)
19 | #endif
20 |
21 | #ifndef OMAGIC
22 | /* Code indicating object file or impure executable. */
23 | #define OMAGIC 0407
24 | /* Code indicating pure executable. */
25 | #define NMAGIC 0410
26 | /* Code indicating demand-paged executable. */
27 | #define ZMAGIC 0413
28 | #endif /* not OMAGIC */
29 |
30 | #ifndef N_BADMAG
31 | #define N_BADMAG(x) \
32 | (N_MAGIC(x) != OMAGIC && N_MAGIC(x) != NMAGIC \
33 | && N_MAGIC(x) != ZMAGIC)
34 | #endif
35 |
36 | #define _N_BADMAG(x) \
37 | (N_MAGIC(x) != OMAGIC && N_MAGIC(x) != NMAGIC \
38 | && N_MAGIC(x) != ZMAGIC)
39 |
40 | #define _N_HDROFF(x) (SEGMENT_SIZE - sizeof (struct exec))
41 |
42 | #ifndef N_TXTOFF
43 | #define N_TXTOFF(x) \
44 | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : sizeof (struct exec))
45 | #endif
46 |
47 | #ifndef N_DATOFF
48 | #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
49 | #endif
50 |
51 | #ifndef N_TRELOFF
52 | #define N_TRELOFF(x) (N_DATOFF(x) + (x).a_data)
53 | #endif
54 |
55 | #ifndef N_DRELOFF
56 | #define N_DRELOFF(x) (N_TRELOFF(x) + (x).a_trsize)
57 | #endif
58 |
59 | #ifndef N_SYMOFF
60 | #define N_SYMOFF(x) (N_DRELOFF(x) + (x).a_drsize)
61 | #endif
62 |
63 | #ifndef N_STROFF
64 | #define N_STROFF(x) (N_SYMOFF(x) + (x).a_syms)
65 | #endif
66 |
67 | /* Address of text segment in memory after it is loaded. */
68 | #ifndef N_TXTADDR
69 | #define N_TXTADDR(x) 0
70 | #endif
71 |
72 | /* Address of data segment in memory after it is loaded.
73 | Note that it is up to you to define SEGMENT_SIZE
74 | on machines not listed here. */
75 | #if defined(vax) || defined(hp300) || defined(pyr)
76 | #define SEGMENT_SIZE PAGE_SIZE
77 | #endif
78 | #ifdef hp300
79 | #define PAGE_SIZE 4096
80 | #endif
81 | #ifdef sony
82 | #define SEGMENT_SIZE 0x2000
83 | #endif /* Sony. */
84 | #ifdef is68k
85 | #define SEGMENT_SIZE 0x20000
86 | #endif
87 | #if defined(m68k) && defined(PORTAR)
88 | #define PAGE_SIZE 0x400
89 | #define SEGMENT_SIZE PAGE_SIZE
90 | #endif
91 |
92 | #define PAGE_SIZE 4096
93 | #define SEGMENT_SIZE 1024
94 |
95 | #define _N_SEGMENT_ROUND(x) (((x) + SEGMENT_SIZE - 1) & ~(SEGMENT_SIZE - 1))
96 |
97 | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
98 |
99 | #ifndef N_DATADDR
100 | #define N_DATADDR(x) \
101 | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
102 | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
103 | #endif
104 |
105 | /* Address of bss segment in memory after it is loaded. */
106 | #ifndef N_BSSADDR
107 | #define N_BSSADDR(x) (N_DATADDR(x) + (x).a_data)
108 | #endif
109 |
110 | #ifndef N_NLIST_DECLARED
111 | struct nlist {
112 | union {
113 | char *n_name;
114 | struct nlist *n_next;
115 | long n_strx;
116 | } n_un;
117 | unsigned char n_type;
118 | char n_other;
119 | short n_desc;
120 | unsigned long n_value;
121 | };
122 | #endif
123 |
124 | #ifndef N_UNDF
125 | #define N_UNDF 0
126 | #endif
127 | #ifndef N_ABS
128 | #define N_ABS 2
129 | #endif
130 | #ifndef N_TEXT
131 | #define N_TEXT 4
132 | #endif
133 | #ifndef N_DATA
134 | #define N_DATA 6
135 | #endif
136 | #ifndef N_BSS
137 | #define N_BSS 8
138 | #endif
139 | #ifndef N_COMM
140 | #define N_COMM 18
141 | #endif
142 | #ifndef N_FN
143 | #define N_FN 15
144 | #endif
145 |
146 | #ifndef N_EXT
147 | #define N_EXT 1
148 | #endif
149 | #ifndef N_TYPE
150 | #define N_TYPE 036
151 | #endif
152 | #ifndef N_STAB
153 | #define N_STAB 0340
154 | #endif
155 |
156 | /* The following type indicates the definition of a symbol as being
157 | an indirect reference to another symbol. The other symbol
158 | appears as an undefined reference, immediately following this symbol.
159 |
160 | Indirection is asymmetrical. The other symbol's value will be used
161 | to satisfy requests for the indirect symbol, but not vice versa.
162 | If the other symbol does not have a definition, libraries will
163 | be searched to find a definition. */
164 | #define N_INDR 0xa
165 |
166 | /* The following symbols refer to set elements.
167 | All the N_SET[ATDB] symbols with the same name form one set.
168 | Space is allocated for the set in the text section, and each set
169 | element's value is stored into one word of the space.
170 | The first word of the space is the length of the set (number of elements).
171 |
172 | The address of the set is made into an N_SETV symbol
173 | whose name is the same as the name of the set.
174 | This symbol acts like a N_DATA global symbol
175 | in that it can satisfy undefined external references. */
176 |
177 | /* These appear as input to LD, in a .o file. */
178 | #define N_SETA 0x14 /* Absolute set element symbol */
179 | #define N_SETT 0x16 /* Text set element symbol */
180 | #define N_SETD 0x18 /* Data set element symbol */
181 | #define N_SETB 0x1A /* Bss set element symbol */
182 |
183 | /* This is output from LD. */
184 | #define N_SETV 0x1C /* Pointer to set vector in data area. */
185 |
186 | #ifndef N_RELOCATION_INFO_DECLARED
187 |
188 | /* This structure describes a single relocation to be performed.
189 | The text-relocation section of the file is a vector of these structures,
190 | all of which apply to the text section.
191 | Likewise, the data-relocation section applies to the data section. */
192 |
193 | struct relocation_info
194 | {
195 | /* Address (within segment) to be relocated. */
196 | int r_address;
197 | /* The meaning of r_symbolnum depends on r_extern. */
198 | unsigned int r_symbolnum:24;
199 | /* Nonzero means value is a pc-relative offset
200 | and it should be relocated for changes in its own address
201 | as well as for changes in the symbol or section specified. */
202 | unsigned int r_pcrel:1;
203 | /* Length (as exponent of 2) of the field to be relocated.
204 | Thus, a value of 2 indicates 1<<2 bytes. */
205 | unsigned int r_length:2;
206 | /* 1 => relocate with value of symbol.
207 | r_symbolnum is the index of the symbol
208 | in file's the symbol table.
209 | 0 => relocate with the address of a segment.
210 | r_symbolnum is N_TEXT, N_DATA, N_BSS or N_ABS
211 | (the N_EXT bit may be set also, but signifies nothing). */
212 | unsigned int r_extern:1;
213 | /* Four bits that aren't used, but when writing an object file
214 | it is desirable to clear them. */
215 | unsigned int r_pad:4;
216 | };
217 | #endif /* no N_RELOCATION_INFO_DECLARED. */
218 |
219 |
220 | #endif /* __A_OUT_GNU_H__ */
221 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/asm/io.h:
--------------------------------------------------------------------------------
1 | #define outb(value,port) \
2 | __asm__ ("outb %%al,%%dx"::"a" (value),"d" (port))
3 |
4 |
5 | #define inb(port) ({ \
6 | unsigned char _v; \
7 | __asm__ volatile ("inb %%dx,%%al":"=a" (_v):"d" (port)); \
8 | _v; \
9 | })
10 |
11 | #define outb_p(value,port) \
12 | __asm__ ("outb %%al,%%dx\n" \
13 | "\tjmp 1f\n" \
14 | "1:\tjmp 1f\n" \
15 | "1:"::"a" (value),"d" (port))
16 |
17 | #define inb_p(port) ({ \
18 | unsigned char _v; \
19 | __asm__ volatile ("inb %%dx,%%al\n" \
20 | "\tjmp 1f\n" \
21 | "1:\tjmp 1f\n" \
22 | "1:":"=a" (_v):"d" (port)); \
23 | _v; \
24 | })
25 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/asm/memory.h:
--------------------------------------------------------------------------------
1 | /*
2 | * NOTE!!! memcpy(dest,src,n) assumes ds=es=normal data segment. This
3 | * goes for all kernel functions (ds=es=kernel space, fs=local data,
4 | * gs=null), as well as for all well-behaving user programs (ds=es=
5 | * user data space). This is NOT a bug, as any user program that changes
6 | * es deserves to die if it isn't careful.
7 | */
8 | //#define memcpy(dest,src,n) ({ \
9 | //void * _res = dest; \
10 | //__asm__ __volatile__ ("cld;rep;movsb" \
11 | // ::"D" ((long)(_res)),"S" ((long)(src)),"c" ((long) (n)) \
12 | // ); \
13 | //_res; \
14 | //})
15 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/asm/segment.h:
--------------------------------------------------------------------------------
1 | static inline unsigned char get_fs_byte(const char * addr)
2 | {
3 | unsigned register char _v;
4 |
5 | __asm__ ("movb %%fs:%1,%0":"=r" (_v):"m" (*addr));
6 | return _v;
7 | }
8 |
9 | static inline unsigned short get_fs_word(const unsigned short *addr)
10 | {
11 | unsigned short _v;
12 |
13 | __asm__ ("movw %%fs:%1,%0":"=r" (_v):"m" (*addr));
14 | return _v;
15 | }
16 |
17 | static inline unsigned long get_fs_long(const unsigned long *addr)
18 | {
19 | unsigned long _v;
20 |
21 | __asm__ ("movl %%fs:%1,%0":"=r" (_v):"m" (*addr)); \
22 | return _v;
23 | }
24 |
25 | static inline void put_fs_byte(char val,char *addr)
26 | {
27 | __asm__ ("movb %0,%%fs:%1"::"r" (val),"m" (*addr));
28 | }
29 |
30 | static inline void put_fs_word(short val,short * addr)
31 | {
32 | __asm__ ("movw %0,%%fs:%1"::"r" (val),"m" (*addr));
33 | }
34 |
35 | static inline void put_fs_long(unsigned long val,unsigned long * addr)
36 | {
37 | __asm__ ("movl %0,%%fs:%1"::"r" (val),"m" (*addr));
38 | }
39 |
40 | /*
41 | * Someone who knows GNU asm better than I should double check the followig.
42 | * It seems to work, but I don't know if I'm doing something subtly wrong.
43 | * --- TYT, 11/24/91
44 | * [ nothing wrong here, Linus ]
45 | */
46 |
47 | static inline unsigned long get_fs()
48 | {
49 | unsigned short _v;
50 | __asm__("mov %%fs,%%ax":"=a" (_v):);
51 | return _v;
52 | }
53 |
54 | static inline unsigned long get_ds()
55 | {
56 | unsigned short _v;
57 | __asm__("mov %%ds,%%ax":"=a" (_v):);
58 | return _v;
59 | }
60 |
61 | static inline void set_fs(unsigned long val)
62 | {
63 | __asm__("mov %0,%%fs"::"a" ((unsigned short) val));
64 | }
65 |
66 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/asm/system.h:
--------------------------------------------------------------------------------
1 | #define move_to_user_mode() \
2 | __asm__ ("movl %%esp,%%eax\n\t" \
3 | "pushl $0x17\n\t" \
4 | "pushl %%eax\n\t" \
5 | "pushfl\n\t" \
6 | "pushl $0x0f\n\t" \
7 | "pushl $1f\n\t" \
8 | "iret\n" \
9 | "1:\tmovl $0x17,%%eax\n\t" \
10 | "movw %%ax,%%ds\n\t" \
11 | "movw %%ax,%%es\n\t" \
12 | "movw %%ax,%%fs\n\t" \
13 | "movw %%ax,%%gs" \
14 | :::"ax")
15 |
16 | #define sti() __asm__ ("sti"::)
17 | #define cli() __asm__ ("cli"::)
18 | #define nop() __asm__ ("nop"::)
19 |
20 | #define iret() __asm__ ("iret"::)
21 |
22 | #define _set_gate(gate_addr,type,dpl,addr) \
23 | __asm__ ("movw %%dx,%%ax\n\t" \
24 | "movw %0,%%dx\n\t" \
25 | "movl %%eax,%1\n\t" \
26 | "movl %%edx,%2" \
27 | : \
28 | : "i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
29 | "o" (*((char *) (gate_addr))), \
30 | "o" (*(4+(char *) (gate_addr))), \
31 | "d" ((char *) (addr)),"a" (0x00080000))
32 |
33 | #define set_intr_gate(n,addr) \
34 | _set_gate(&idt[n],14,0,addr)
35 |
36 | #define set_trap_gate(n,addr) \
37 | _set_gate(&idt[n],15,0,addr)
38 |
39 | #define set_system_gate(n,addr) \
40 | _set_gate(&idt[n],15,3,addr)
41 |
42 | #define _set_seg_desc(gate_addr,type,dpl,base,limit) {\
43 | *(gate_addr) = ((base) & 0xff000000) | \
44 | (((base) & 0x00ff0000)>>16) | \
45 | ((limit) & 0xf0000) | \
46 | ((dpl)<<13) | \
47 | (0x00408000) | \
48 | ((type)<<8); \
49 | *((gate_addr)+1) = (((base) & 0x0000ffff)<<16) | \
50 | ((limit) & 0x0ffff); }
51 |
52 | #define _set_tssldt_desc(n,addr,type) \
53 | __asm__ ("movw $104,%1\n\t" \
54 | "movw %%ax,%2\n\t" \
55 | "rorl $16,%%eax\n\t" \
56 | "movb %%al,%3\n\t" \
57 | "movb $" type ",%4\n\t" \
58 | "movb $0x00,%5\n\t" \
59 | "movb %%ah,%6\n\t" \
60 | "rorl $16,%%eax" \
61 | ::"a" (addr), "m" (*(n)), "m" (*(n+2)), "m" (*(n+4)), \
62 | "m" (*(n+5)), "m" (*(n+6)), "m" (*(n+7)) \
63 | )
64 |
65 | #define set_tss_desc(n,addr) _set_tssldt_desc(((char *) (n)),((int)(addr)),"0x89")
66 | #define set_ldt_desc(n,addr) _set_tssldt_desc(((char *) (n)),((int)(addr)),"0x82")
67 |
68 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/const.h:
--------------------------------------------------------------------------------
1 | #ifndef _CONST_H
2 | #define _CONST_H
3 |
4 | #define BUFFER_END 0x200000
5 |
6 | #define I_TYPE 0170000
7 | #define I_DIRECTORY 0040000
8 | #define I_REGULAR 0100000
9 | #define I_BLOCK_SPECIAL 0060000
10 | #define I_CHAR_SPECIAL 0020000
11 | #define I_NAMED_PIPE 0010000
12 | #define I_SET_UID_BIT 0004000
13 | #define I_SET_GID_BIT 0002000
14 |
15 | #endif
16 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/ctype.h:
--------------------------------------------------------------------------------
1 | #ifndef _CTYPE_H
2 | #define _CTYPE_H
3 |
4 | #define _U 0x01 /* upper */
5 | #define _L 0x02 /* lower */
6 | #define _D 0x04 /* digit */
7 | #define _C 0x08 /* cntrl */
8 | #define _P 0x10 /* punct */
9 | #define _S 0x20 /* white space (space/lf/tab) */
10 | #define _X 0x40 /* hex digit */
11 | #define _SP 0x80 /* hard space (0x20) */
12 |
13 | extern unsigned char _ctype[];
14 | extern char _ctmp;
15 |
16 | #define isalnum(c) ((_ctype+1)[c]&(_U|_L|_D))
17 | #define isalpha(c) ((_ctype+1)[c]&(_U|_L))
18 | #define iscntrl(c) ((_ctype+1)[c]&(_C))
19 | #define isdigit(c) ((_ctype+1)[c]&(_D))
20 | #define isgraph(c) ((_ctype+1)[c]&(_P|_U|_L|_D))
21 | #define islower(c) ((_ctype+1)[c]&(_L))
22 | #define isprint(c) ((_ctype+1)[c]&(_P|_U|_L|_D|_SP))
23 | #define ispunct(c) ((_ctype+1)[c]&(_P))
24 | #define isspace(c) ((_ctype+1)[c]&(_S))
25 | #define isupper(c) ((_ctype+1)[c]&(_U))
26 | #define isxdigit(c) ((_ctype+1)[c]&(_D|_X))
27 |
28 | #define isascii(c) (((unsigned) c)<=0x7f)
29 | #define toascii(c) (((unsigned) c)&0x7f)
30 |
31 | #define tolower(c) (_ctmp=c,isupper(_ctmp)?_ctmp-('A'-'a'):_ctmp)
32 | #define toupper(c) (_ctmp=c,islower(_ctmp)?_ctmp-('a'-'A'):_ctmp)
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/errno.h:
--------------------------------------------------------------------------------
1 | #ifndef _ERRNO_H
2 | #define _ERRNO_H
3 |
4 | /*
5 | * ok, as I hadn't got any other source of information about
6 | * possible error numbers, I was forced to use the same numbers
7 | * as minix.
8 | * Hopefully these are posix or something. I wouldn't know (and posix
9 | * isn't telling me - they want $$$ for their f***ing standard).
10 | *
11 | * We don't use the _SIGN cludge of minix, so kernel returns must
12 | * see to the sign by themselves.
13 | *
14 | * NOTE! Remember to change strerror() if you change this file!
15 | */
16 |
17 | extern int errno;
18 |
19 | #define ERROR 99
20 | #define EPERM 1
21 | #define ENOENT 2
22 | #define ESRCH 3
23 | #define EINTR 4
24 | #define EIO 5
25 | #define ENXIO 6
26 | #define E2BIG 7
27 | #define ENOEXEC 8
28 | #define EBADF 9
29 | #define ECHILD 10
30 | #define EAGAIN 11
31 | #define ENOMEM 12
32 | #define EACCES 13
33 | #define EFAULT 14
34 | #define ENOTBLK 15
35 | #define EBUSY 16
36 | #define EEXIST 17
37 | #define EXDEV 18
38 | #define ENODEV 19
39 | #define ENOTDIR 20
40 | #define EISDIR 21
41 | #define EINVAL 22
42 | #define ENFILE 23
43 | #define EMFILE 24
44 | #define ENOTTY 25
45 | #define ETXTBSY 26
46 | #define EFBIG 27
47 | #define ENOSPC 28
48 | #define ESPIPE 29
49 | #define EROFS 30
50 | #define EMLINK 31
51 | #define EPIPE 32
52 | #define EDOM 33
53 | #define ERANGE 34
54 | #define EDEADLK 35
55 | #define ENAMETOOLONG 36
56 | #define ENOLCK 37
57 | #define ENOSYS 38
58 | #define ENOTEMPTY 39
59 |
60 | #endif
61 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/fcntl.h:
--------------------------------------------------------------------------------
1 | #ifndef _FCNTL_H
2 | #define _FCNTL_H
3 |
4 | #include
5 |
6 | /* open/fcntl - NOCTTY, NDELAY isn't implemented yet */
7 | #define O_ACCMODE 00003
8 | #define O_RDONLY 00
9 | #define O_WRONLY 01
10 | #define O_RDWR 02
11 | #define O_CREAT 00100 /* not fcntl */
12 | #define O_EXCL 00200 /* not fcntl */
13 | #define O_NOCTTY 00400 /* not fcntl */
14 | #define O_TRUNC 01000 /* not fcntl */
15 | #define O_APPEND 02000
16 | #define O_NONBLOCK 04000 /* not fcntl */
17 | #define O_NDELAY O_NONBLOCK
18 |
19 | /* Defines for fcntl-commands. Note that currently
20 | * locking isn't supported, and other things aren't really
21 | * tested.
22 | */
23 | #define F_DUPFD 0 /* dup */
24 | #define F_GETFD 1 /* get f_flags */
25 | #define F_SETFD 2 /* set f_flags */
26 | #define F_GETFL 3 /* more flags (cloexec) */
27 | #define F_SETFL 4
28 | #define F_GETLK 5 /* not implemented */
29 | #define F_SETLK 6
30 | #define F_SETLKW 7
31 |
32 | /* for F_[GET|SET]FL */
33 | #define FD_CLOEXEC 1 /* actually anything with low bit set goes */
34 |
35 | /* Ok, these are locking features, and aren't implemented at any
36 | * level. POSIX wants them.
37 | */
38 | #define F_RDLCK 0
39 | #define F_WRLCK 1
40 | #define F_UNLCK 2
41 |
42 | /* Once again - not implemented, but ... */
43 | struct flock {
44 | short l_type;
45 | short l_whence;
46 | off_t l_start;
47 | off_t l_len;
48 | pid_t l_pid;
49 | };
50 |
51 | extern int creat(const char * filename,mode_t mode);
52 | extern int fcntl(int fildes,int cmd, ...);
53 | extern int open(const char * filename, int flags, ...);
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/config.h:
--------------------------------------------------------------------------------
1 | #ifndef _CONFIG_H
2 | #define _CONFIG_H
3 |
4 | /*
5 | * The root-device is no longer hard-coded. You can change the default
6 | * root-device by changing the line ROOT_DEV = XXX in boot/bootsect.s
7 | */
8 |
9 | /*
10 | * define your keyboard here -
11 | * KBD_FINNISH for Finnish keyboards
12 | * KBD_US for US-type
13 | * KBD_GR for German keyboards
14 | * KBD_FR for Frech keyboard
15 | */
16 | #define KBD_US
17 | /*#define KBD_GR */
18 | /*#define KBD_FR */
19 | /*#define KBD_FINNISH */
20 |
21 | /*
22 | * Normally, Linux can get the drive parameters from the BIOS at
23 | * startup, but if this for some unfathomable reason fails, you'd
24 | * be left stranded. For this case, you can define HD_TYPE, which
25 | * contains all necessary info on your harddisk.
26 | *
27 | * The HD_TYPE macro should look like this:
28 | *
29 | * #define HD_TYPE { head, sect, cyl, wpcom, lzone, ctl}
30 | *
31 | * In case of two harddisks, the info should be sepatated by
32 | * commas:
33 | *
34 | * #define HD_TYPE { h,s,c,wpcom,lz,ctl },{ h,s,c,wpcom,lz,ctl }
35 | */
36 | /*
37 | This is an example, two drives, first is type 2, second is type 3:
38 |
39 | #define HD_TYPE { 4,17,615,300,615,8 }, { 6,17,615,300,615,0 }
40 |
41 | NOTE: ctl is 0 for all drives with heads<=8, and ctl=8 for drives
42 | with more than 8 heads.
43 |
44 | If you want the BIOS to tell what kind of drive you have, just
45 | leave HD_TYPE undefined. This is the normal thing to do.
46 | */
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/fdreg.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file contains some defines for the floppy disk controller.
3 | * Various sources. Mostly "IBM Microcomputers: A Programmers
4 | * Handbook", Sanches and Canton.
5 | */
6 | #ifndef _FDREG_H
7 | #define _FDREG_H
8 |
9 | extern int ticks_to_floppy_on(unsigned int nr);
10 | extern void floppy_on(unsigned int nr);
11 | extern void floppy_off(unsigned int nr);
12 | extern void floppy_select(unsigned int nr);
13 | extern void floppy_deselect(unsigned int nr);
14 |
15 | /* Fd controller regs. S&C, about page 340 */
16 | #define FD_STATUS 0x3f4
17 | #define FD_DATA 0x3f5
18 | #define FD_DOR 0x3f2 /* Digital Output Register */
19 | #define FD_DIR 0x3f7 /* Digital Input Register (read) */
20 | #define FD_DCR 0x3f7 /* Diskette Control Register (write)*/
21 |
22 | /* Bits of main status register */
23 | #define STATUS_BUSYMASK 0x0F /* drive busy mask */
24 | #define STATUS_BUSY 0x10 /* FDC busy */
25 | #define STATUS_DMA 0x20 /* 0- DMA mode */
26 | #define STATUS_DIR 0x40 /* 0- cpu->fdc */
27 | #define STATUS_READY 0x80 /* Data reg ready */
28 |
29 | /* Bits of FD_ST0 */
30 | #define ST0_DS 0x03 /* drive select mask */
31 | #define ST0_HA 0x04 /* Head (Address) */
32 | #define ST0_NR 0x08 /* Not Ready */
33 | #define ST0_ECE 0x10 /* Equipment chech error */
34 | #define ST0_SE 0x20 /* Seek end */
35 | #define ST0_INTR 0xC0 /* Interrupt code mask */
36 |
37 | /* Bits of FD_ST1 */
38 | #define ST1_MAM 0x01 /* Missing Address Mark */
39 | #define ST1_WP 0x02 /* Write Protect */
40 | #define ST1_ND 0x04 /* No Data - unreadable */
41 | #define ST1_OR 0x10 /* OverRun */
42 | #define ST1_CRC 0x20 /* CRC error in data or addr */
43 | #define ST1_EOC 0x80 /* End Of Cylinder */
44 |
45 | /* Bits of FD_ST2 */
46 | #define ST2_MAM 0x01 /* Missing Addess Mark (again) */
47 | #define ST2_BC 0x02 /* Bad Cylinder */
48 | #define ST2_SNS 0x04 /* Scan Not Satisfied */
49 | #define ST2_SEH 0x08 /* Scan Equal Hit */
50 | #define ST2_WC 0x10 /* Wrong Cylinder */
51 | #define ST2_CRC 0x20 /* CRC error in data field */
52 | #define ST2_CM 0x40 /* Control Mark = deleted */
53 |
54 | /* Bits of FD_ST3 */
55 | #define ST3_HA 0x04 /* Head (Address) */
56 | #define ST3_TZ 0x10 /* Track Zero signal (1=track 0) */
57 | #define ST3_WP 0x40 /* Write Protect */
58 |
59 | /* Values for FD_COMMAND */
60 | #define FD_RECALIBRATE 0x07 /* move to track 0 */
61 | #define FD_SEEK 0x0F /* seek track */
62 | #define FD_READ 0xE6 /* read with MT, MFM, SKip deleted */
63 | #define FD_WRITE 0xC5 /* write with MT, MFM */
64 | #define FD_SENSEI 0x08 /* Sense Interrupt Status */
65 | #define FD_SPECIFY 0x03 /* specify HUT etc */
66 |
67 | /* DMA commands */
68 | #define DMA_READ 0x46
69 | #define DMA_WRITE 0x4A
70 |
71 | #endif
72 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/fs.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file has definitions for some important file table
3 | * structures etc.
4 | */
5 |
6 | #ifndef _FS_H
7 | #define _FS_H
8 |
9 | #include
10 |
11 | /* devices are as follows: (same as minix, so we can use the minix
12 | * file system. These are major numbers.)
13 | *
14 | * 0 - unused (nodev)
15 | * 1 - /dev/mem
16 | * 2 - /dev/fd
17 | * 3 - /dev/hd
18 | * 4 - /dev/ttyx
19 | * 5 - /dev/tty
20 | * 6 - /dev/lp
21 | * 7 - unnamed pipes
22 | */
23 |
24 | #define IS_SEEKABLE(x) ((x)>=1 && (x)<=3)
25 |
26 | #define READ 0
27 | #define WRITE 1
28 | #define READA 2 /* read-ahead - don't pause */
29 | #define WRITEA 3 /* "write-ahead" - silly, but somewhat useful */
30 |
31 | void buffer_init(long buffer_end);
32 |
33 | #define MAJOR(a) (((unsigned)(a))>>8)
34 | #define MINOR(a) ((a)&0xff)
35 |
36 | #define NAME_LEN 14
37 | #define ROOT_INO 1
38 |
39 | #define I_MAP_SLOTS 8
40 | #define Z_MAP_SLOTS 8
41 | #define SUPER_MAGIC 0x137F
42 |
43 | #define NR_OPEN 20
44 | #define NR_INODE 32
45 | #define NR_FILE 64
46 | #define NR_SUPER 8
47 | #define NR_HASH 307
48 | #define NR_BUFFERS nr_buffers
49 | #define BLOCK_SIZE 1024
50 | #define BLOCK_SIZE_BITS 10
51 | #ifndef NULL
52 | #define NULL ((void *) 0)
53 | #endif
54 |
55 | #define INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct d_inode)))
56 | #define DIR_ENTRIES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct dir_entry)))
57 |
58 | #define PIPE_HEAD(inode) ((inode).i_zone[0])
59 | #define PIPE_TAIL(inode) ((inode).i_zone[1])
60 | #define PIPE_SIZE(inode) ((PIPE_HEAD(inode)-PIPE_TAIL(inode))&(PAGE_SIZE-1))
61 | #define PIPE_EMPTY(inode) (PIPE_HEAD(inode)==PIPE_TAIL(inode))
62 | #define PIPE_FULL(inode) (PIPE_SIZE(inode)==(PAGE_SIZE-1))
63 | #define INC_PIPE(head) \
64 | __asm__("incl %0\n\tandl $4095,%0"::"m" (head))
65 |
66 | typedef char buffer_block[BLOCK_SIZE];
67 |
68 | struct buffer_head {
69 | char * b_data; /* pointer to data block (1024 bytes) */
70 | unsigned long b_blocknr; /* block number */
71 | unsigned short b_dev; /* device (0 = free) */
72 | unsigned char b_uptodate;
73 | unsigned char b_dirt; /* 0-clean,1-dirty */
74 | unsigned char b_count; /* users using this block */
75 | unsigned char b_lock; /* 0 - ok, 1 -locked */
76 | struct task_struct * b_wait;
77 | struct buffer_head * b_prev;
78 | struct buffer_head * b_next;
79 | struct buffer_head * b_prev_free;
80 | struct buffer_head * b_next_free;
81 | };
82 |
83 | struct d_inode {
84 | unsigned short i_mode;
85 | unsigned short i_uid;
86 | unsigned long i_size;
87 | unsigned long i_time;
88 | unsigned char i_gid;
89 | unsigned char i_nlinks;
90 | unsigned short i_zone[9];
91 | };
92 |
93 | struct m_inode {
94 | unsigned short i_mode;
95 | unsigned short i_uid;
96 | unsigned long i_size;
97 | unsigned long i_mtime;
98 | unsigned char i_gid;
99 | unsigned char i_nlinks;
100 | unsigned short i_zone[9];
101 | /* these are in memory also */
102 | struct task_struct * i_wait;
103 | unsigned long i_atime;
104 | unsigned long i_ctime;
105 | unsigned short i_dev;
106 | unsigned short i_num;
107 | unsigned short i_count;
108 | unsigned char i_lock;
109 | unsigned char i_dirt;
110 | unsigned char i_pipe;
111 | unsigned char i_mount;
112 | unsigned char i_seek;
113 | unsigned char i_update;
114 | };
115 |
116 | struct file {
117 | unsigned short f_mode;
118 | unsigned short f_flags;
119 | unsigned short f_count;
120 | struct m_inode * f_inode;
121 | off_t f_pos;
122 | };
123 |
124 | struct super_block {
125 | unsigned short s_ninodes;
126 | unsigned short s_nzones;
127 | unsigned short s_imap_blocks;
128 | unsigned short s_zmap_blocks;
129 | unsigned short s_firstdatazone;
130 | unsigned short s_log_zone_size;
131 | unsigned long s_max_size;
132 | unsigned short s_magic;
133 | /* These are only in memory */
134 | struct buffer_head * s_imap[8];
135 | struct buffer_head * s_zmap[8];
136 | unsigned short s_dev;
137 | struct m_inode * s_isup;
138 | struct m_inode * s_imount;
139 | unsigned long s_time;
140 | struct task_struct * s_wait;
141 | unsigned char s_lock;
142 | unsigned char s_rd_only;
143 | unsigned char s_dirt;
144 | };
145 |
146 | struct d_super_block {
147 | unsigned short s_ninodes;
148 | unsigned short s_nzones;
149 | unsigned short s_imap_blocks;
150 | unsigned short s_zmap_blocks;
151 | unsigned short s_firstdatazone;
152 | unsigned short s_log_zone_size;
153 | unsigned long s_max_size;
154 | unsigned short s_magic;
155 | };
156 |
157 | struct dir_entry {
158 | unsigned short inode;
159 | char name[NAME_LEN];
160 | };
161 |
162 | extern struct m_inode inode_table[NR_INODE];
163 | extern struct file file_table[NR_FILE];
164 | extern struct super_block super_block[NR_SUPER];
165 | extern struct buffer_head * start_buffer;
166 | extern int nr_buffers;
167 |
168 | extern void check_disk_change(int dev);
169 | extern int floppy_change(unsigned int nr);
170 | extern int ticks_to_floppy_on(unsigned int dev);
171 | extern void floppy_on(unsigned int dev);
172 | extern void floppy_off(unsigned int dev);
173 | extern void truncate(struct m_inode * inode);
174 | extern void sync_inodes(void);
175 | extern void wait_on(struct m_inode * inode);
176 | extern int bmap(struct m_inode * inode,int block);
177 | extern int create_block(struct m_inode * inode,int block);
178 | extern struct m_inode * namei(const char * pathname);
179 | extern int open_namei(const char * pathname, int flag, int mode,
180 | struct m_inode ** res_inode);
181 | extern void iput(struct m_inode * inode);
182 | extern struct m_inode * iget(int dev,int nr);
183 | extern struct m_inode * get_empty_inode(void);
184 | extern struct m_inode * get_pipe_inode(void);
185 | extern struct buffer_head * get_hash_table(int dev, int block);
186 | extern struct buffer_head * getblk(int dev, int block);
187 | extern void ll_rw_block(int rw, struct buffer_head * bh);
188 | extern void brelse(struct buffer_head * buf);
189 | extern struct buffer_head * bread(int dev,int block);
190 | extern void bread_page(unsigned long addr,int dev,int b[4]);
191 | extern struct buffer_head * breada(int dev,int block,...);
192 | extern int new_block(int dev);
193 | extern void free_block(int dev, int block);
194 | extern struct m_inode * new_inode(int dev);
195 | extern void free_inode(struct m_inode * inode);
196 | extern int sync_dev(int dev);
197 | extern struct super_block * get_super(int dev);
198 | extern int ROOT_DEV;
199 |
200 | extern void mount_root(void);
201 |
202 | #endif
203 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/hdreg.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file contains some defines for the AT-hd-controller.
3 | * Various sources. Check out some definitions (see comments with
4 | * a ques).
5 | */
6 | #ifndef _HDREG_H
7 | #define _HDREG_H
8 |
9 | /* Hd controller regs. Ref: IBM AT Bios-listing */
10 | #define HD_DATA 0x1f0 /* _CTL when writing */
11 | #define HD_ERROR 0x1f1 /* see err-bits */
12 | #define HD_NSECTOR 0x1f2 /* nr of sectors to read/write */
13 | #define HD_SECTOR 0x1f3 /* starting sector */
14 | #define HD_LCYL 0x1f4 /* starting cylinder */
15 | #define HD_HCYL 0x1f5 /* high byte of starting cyl */
16 | #define HD_CURRENT 0x1f6 /* 101dhhhh , d=drive, hhhh=head */
17 | #define HD_STATUS 0x1f7 /* see status-bits */
18 | #define HD_PRECOMP HD_ERROR /* same io address, read=error, write=precomp */
19 | #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */
20 |
21 | #define HD_CMD 0x3f6
22 |
23 | /* Bits of HD_STATUS */
24 | #define ERR_STAT 0x01
25 | #define INDEX_STAT 0x02
26 | #define ECC_STAT 0x04 /* Corrected error */
27 | #define DRQ_STAT 0x08
28 | #define SEEK_STAT 0x10
29 | #define WRERR_STAT 0x20
30 | #define READY_STAT 0x40
31 | #define BUSY_STAT 0x80
32 |
33 | /* Values for HD_COMMAND */
34 | #define WIN_RESTORE 0x10
35 | #define WIN_READ 0x20
36 | #define WIN_WRITE 0x30
37 | #define WIN_VERIFY 0x40
38 | #define WIN_FORMAT 0x50
39 | #define WIN_INIT 0x60
40 | #define WIN_SEEK 0x70
41 | #define WIN_DIAGNOSE 0x90
42 | #define WIN_SPECIFY 0x91
43 |
44 | /* Bits for HD_ERROR */
45 | #define MARK_ERR 0x01 /* Bad address mark ? */
46 | #define TRK0_ERR 0x02 /* couldn't find track 0 */
47 | #define ABRT_ERR 0x04 /* ? */
48 | #define ID_ERR 0x10 /* ? */
49 | #define ECC_ERR 0x40 /* ? */
50 | #define BBD_ERR 0x80 /* ? */
51 |
52 | struct partition {
53 | unsigned char boot_ind; /* 0x80 - active (unused) */
54 | unsigned char head; /* ? */
55 | unsigned char sector; /* ? */
56 | unsigned char cyl; /* ? */
57 | unsigned char sys_ind; /* ? */
58 | unsigned char end_head; /* ? */
59 | unsigned char end_sector; /* ? */
60 | unsigned char end_cyl; /* ? */
61 | unsigned int start_sect; /* starting sector counting from 0 */
62 | unsigned int nr_sects; /* nr of sectors in partition */
63 | };
64 |
65 | #endif
66 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/head.h:
--------------------------------------------------------------------------------
1 | #ifndef _HEAD_H
2 | #define _HEAD_H
3 |
4 | typedef struct desc_struct {
5 | unsigned long a,b;
6 | } desc_table[256];
7 |
8 | extern unsigned long pg_dir[1024];
9 | extern desc_table idt,gdt;
10 |
11 | #define GDT_NUL 0
12 | #define GDT_CODE 1
13 | #define GDT_DATA 2
14 | #define GDT_TMP 3
15 |
16 | #define LDT_NUL 0
17 | #define LDT_CODE 1
18 | #define LDT_DATA 2
19 |
20 | #endif
21 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/kernel.h:
--------------------------------------------------------------------------------
1 | /*
2 | * 'kernel.h' contains some often-used function prototypes etc
3 | */
4 | void verify_area(void * addr,int count);
5 | void panic(const char * str);
6 | int printf(const char * fmt, ...);
7 | int printk(const char * fmt, ...);
8 | int tty_write(unsigned ch,char * buf,int count);
9 | void * malloc(unsigned int size);
10 | void free_s(void * obj, int size);
11 |
12 | #define free(x) free_s((x), 0)
13 |
14 | /*
15 | * This is defined as a macro, but at some point this might become a
16 | * real subroutine that sets a flag if it returns true (to do
17 | * BSD-style accounting where the process is flagged if it uses root
18 | * privs). The implication of this is that you should do normal
19 | * permissions checks first, and check suser() last.
20 | */
21 | #define suser() (current->euid == 0)
22 |
23 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/mm.h:
--------------------------------------------------------------------------------
1 | #ifndef _MM_H
2 | #define _MM_H
3 |
4 | #define PAGE_SIZE 4096
5 |
6 | extern unsigned long get_free_page(void);
7 | extern unsigned long put_page(unsigned long page,unsigned long address);
8 | extern void free_page(unsigned long addr);
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/sys.h:
--------------------------------------------------------------------------------
1 | extern int sys_setup();
2 | extern int sys_exit();
3 | extern int sys_fork();
4 | extern int sys_read();
5 | extern int sys_write();
6 | extern int sys_open();
7 | extern int sys_close();
8 | extern int sys_waitpid();
9 | extern int sys_creat();
10 | extern int sys_link();
11 | extern int sys_unlink();
12 | extern int sys_execve();
13 | extern int sys_chdir();
14 | extern int sys_time();
15 | extern int sys_mknod();
16 | extern int sys_chmod();
17 | extern int sys_chown();
18 | extern int sys_break();
19 | extern int sys_stat();
20 | extern int sys_lseek();
21 | extern int sys_getpid();
22 | extern int sys_mount();
23 | extern int sys_umount();
24 | extern int sys_setuid();
25 | extern int sys_getuid();
26 | extern int sys_stime();
27 | extern int sys_ptrace();
28 | extern int sys_alarm();
29 | extern int sys_fstat();
30 | extern int sys_pause();
31 | extern int sys_utime();
32 | extern int sys_stty();
33 | extern int sys_gtty();
34 | extern int sys_access();
35 | extern int sys_nice();
36 | extern int sys_ftime();
37 | extern int sys_sync();
38 | extern int sys_kill();
39 | extern int sys_rename();
40 | extern int sys_mkdir();
41 | extern int sys_rmdir();
42 | extern int sys_dup();
43 | extern int sys_pipe();
44 | extern int sys_times();
45 | extern int sys_prof();
46 | extern int sys_brk();
47 | extern int sys_setgid();
48 | extern int sys_getgid();
49 | extern int sys_signal();
50 | extern int sys_geteuid();
51 | extern int sys_getegid();
52 | extern int sys_acct();
53 | extern int sys_phys();
54 | extern int sys_lock();
55 | extern int sys_ioctl();
56 | extern int sys_fcntl();
57 | extern int sys_mpx();
58 | extern int sys_setpgid();
59 | extern int sys_ulimit();
60 | extern int sys_uname();
61 | extern int sys_umask();
62 | extern int sys_chroot();
63 | extern int sys_ustat();
64 | extern int sys_dup2();
65 | extern int sys_getppid();
66 | extern int sys_getpgrp();
67 | extern int sys_setsid();
68 | extern int sys_sigaction();
69 | extern int sys_sgetmask();
70 | extern int sys_ssetmask();
71 | extern int sys_setreuid();
72 | extern int sys_setregid();
73 | extern int sys_print_val(int a);
74 | extern int sys_str2num(char *str, int str_len, long *ret);
75 |
76 | fn_ptr sys_call_table[] = { sys_setup, sys_exit, sys_fork, sys_read,
77 | sys_write, sys_open, sys_close, sys_waitpid, sys_creat, sys_link,
78 | sys_unlink, sys_execve, sys_chdir, sys_time, sys_mknod, sys_chmod,
79 | sys_chown, sys_break, sys_stat, sys_lseek, sys_getpid, sys_mount,
80 | sys_umount, sys_setuid, sys_getuid, sys_stime, sys_ptrace, sys_alarm,
81 | sys_fstat, sys_pause, sys_utime, sys_stty, sys_gtty, sys_access,
82 | sys_nice, sys_ftime, sys_sync, sys_kill, sys_rename, sys_mkdir,
83 | sys_rmdir, sys_dup, sys_pipe, sys_times, sys_prof, sys_brk, sys_setgid,
84 | sys_getgid, sys_signal, sys_geteuid, sys_getegid, sys_acct, sys_phys,
85 | sys_lock, sys_ioctl, sys_fcntl, sys_mpx, sys_setpgid, sys_ulimit,
86 | sys_uname, sys_umask, sys_chroot, sys_ustat, sys_dup2, sys_getppid,
87 | sys_getpgrp, sys_setsid, sys_sigaction, sys_sgetmask, sys_ssetmask,
88 | sys_setreuid,sys_setregid,sys_print_val,sys_str2num };
89 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/linux/tty.h:
--------------------------------------------------------------------------------
1 | /*
2 | * 'tty.h' defines some structures used by tty_io.c and some defines.
3 | *
4 | * NOTE! Don't touch this without checking that nothing in rs_io.s or
5 | * con_io.s breaks. Some constants are hardwired into the system (mainly
6 | * offsets into 'tty_queue'
7 | */
8 |
9 | #ifndef _TTY_H
10 | #define _TTY_H
11 |
12 | #include
13 |
14 | #define TTY_BUF_SIZE 1024
15 |
16 | struct tty_queue {
17 | unsigned long data;
18 | unsigned long head;
19 | unsigned long tail;
20 | struct task_struct * proc_list;
21 | char buf[TTY_BUF_SIZE];
22 | };
23 |
24 | #define INC(a) ((a) = ((a)+1) & (TTY_BUF_SIZE-1))
25 | #define DEC(a) ((a) = ((a)-1) & (TTY_BUF_SIZE-1))
26 | #define EMPTY(a) ((a).head == (a).tail)
27 | #define LEFT(a) (((a).tail-(a).head-1)&(TTY_BUF_SIZE-1))
28 | #define LAST(a) ((a).buf[(TTY_BUF_SIZE-1)&((a).head-1)])
29 | #define FULL(a) (!LEFT(a))
30 | #define CHARS(a) (((a).head-(a).tail)&(TTY_BUF_SIZE-1))
31 | #define GETCH(queue,c) \
32 | (void)({c=(queue).buf[(queue).tail];INC((queue).tail);})
33 | #define PUTCH(c,queue) \
34 | (void)({(queue).buf[(queue).head]=(c);INC((queue).head);})
35 |
36 | #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
37 | #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
38 | #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
39 | #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
40 | #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
41 | #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
42 | #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
43 | #define SUSPEND_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
44 |
45 | struct tty_struct {
46 | struct termios termios;
47 | int pgrp;
48 | int stopped;
49 | void (*write)(struct tty_struct * tty);
50 | struct tty_queue read_q;
51 | struct tty_queue write_q;
52 | struct tty_queue secondary;
53 | };
54 |
55 | extern struct tty_struct tty_table[];
56 |
57 | /* intr=^C quit=^| erase=del kill=^U
58 | eof=^D vtime=\0 vmin=\1 sxtc=\0
59 | start=^Q stop=^S susp=^Z eol=\0
60 | reprint=^R discard=^U werase=^W lnext=^V
61 | eol2=\0
62 | */
63 | #define INIT_C_CC "\003\034\177\025\004\0\1\0\021\023\032\0\022\017\027\026\0"
64 |
65 | void rs_init(void);
66 | void con_init(void);
67 | void tty_init(void);
68 |
69 | int tty_read(unsigned c, char * buf, int n);
70 | int tty_write(unsigned c, char * buf, int n);
71 |
72 | void rs_write(struct tty_struct * tty);
73 | void con_write(struct tty_struct * tty);
74 |
75 | void copy_to_cooked(struct tty_struct * tty);
76 |
77 | #endif
78 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/signal.h:
--------------------------------------------------------------------------------
1 | #ifndef _SIGNAL_H
2 | #define _SIGNAL_H
3 |
4 | #include
5 |
6 | typedef int sig_atomic_t;
7 | typedef unsigned int sigset_t; /* 32 bits */
8 |
9 | #define _NSIG 32
10 | #define NSIG _NSIG
11 |
12 | #define SIGHUP 1
13 | #define SIGINT 2
14 | #define SIGQUIT 3
15 | #define SIGILL 4
16 | #define SIGTRAP 5
17 | #define SIGABRT 6
18 | #define SIGIOT 6
19 | #define SIGUNUSED 7
20 | #define SIGFPE 8
21 | #define SIGKILL 9
22 | #define SIGUSR1 10
23 | #define SIGSEGV 11
24 | #define SIGUSR2 12
25 | #define SIGPIPE 13
26 | #define SIGALRM 14
27 | #define SIGTERM 15
28 | #define SIGSTKFLT 16
29 | #define SIGCHLD 17
30 | #define SIGCONT 18
31 | #define SIGSTOP 19
32 | #define SIGTSTP 20
33 | #define SIGTTIN 21
34 | #define SIGTTOU 22
35 |
36 | /* Ok, I haven't implemented sigactions, but trying to keep headers POSIX */
37 | #define SA_NOCLDSTOP 1
38 | #define SA_NOMASK 0x40000000
39 | #define SA_ONESHOT 0x80000000
40 |
41 | #define SIG_BLOCK 0 /* for blocking signals */
42 | #define SIG_UNBLOCK 1 /* for unblocking signals */
43 | #define SIG_SETMASK 2 /* for setting the signal mask */
44 |
45 | #define SIG_DFL ((void (*)(int))0) /* default signal handling */
46 | #define SIG_IGN ((void (*)(int))1) /* ignore signal */
47 |
48 | struct sigaction {
49 | void (*sa_handler)(int);
50 | sigset_t sa_mask;
51 | int sa_flags;
52 | void (*sa_restorer)(void);
53 | };
54 |
55 | void (*signal(int _sig, void (*_func)(int)))(int);
56 | int raise(int sig);
57 | int kill(pid_t pid, int sig);
58 | int sigaddset(sigset_t *mask, int signo);
59 | int sigdelset(sigset_t *mask, int signo);
60 | int sigemptyset(sigset_t *mask);
61 | int sigfillset(sigset_t *mask);
62 | int sigismember(sigset_t *mask, int signo); /* 1 - is, 0 - not, -1 error */
63 | int sigpending(sigset_t *set);
64 | int sigprocmask(int how, sigset_t *set, sigset_t *oldset);
65 | int sigsuspend(sigset_t *sigmask);
66 | int sigaction(int sig, struct sigaction *act, struct sigaction *oldact);
67 |
68 | #endif /* _SIGNAL_H */
69 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/stdarg.h:
--------------------------------------------------------------------------------
1 | #ifndef _STDARG_H
2 | #define _STDARG_H
3 |
4 | typedef char *va_list;
5 |
6 | /* Amount of space required in an argument list for an arg of type TYPE.
7 | TYPE may alternatively be an expression whose type is used. */
8 |
9 | #define __va_rounded_size(TYPE) \
10 | (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
11 |
12 | #ifndef __sparc__
13 | #define va_start(AP, LASTARG) \
14 | (AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
15 | #else
16 | #define va_start(AP, LASTARG) \
17 | (__builtin_saveregs (), \
18 | AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
19 | #endif
20 |
21 | void va_end (va_list); /* Defined in gnulib */
22 | #define va_end(AP)
23 |
24 | #define va_arg(AP, TYPE) \
25 | (AP += __va_rounded_size (TYPE), \
26 | *((TYPE *) (AP - __va_rounded_size (TYPE))))
27 |
28 | #endif /* _STDARG_H */
29 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/stddef.h:
--------------------------------------------------------------------------------
1 | #ifndef _STDDEF_H
2 | #define _STDDEF_H
3 |
4 | #ifndef _PTRDIFF_T
5 | #define _PTRDIFF_T
6 | typedef long ptrdiff_t;
7 | #endif
8 |
9 | #ifndef _SIZE_T
10 | #define _SIZE_T
11 | typedef unsigned long size_t;
12 | #endif
13 |
14 | #undef NULL
15 | #define NULL ((void *)0)
16 |
17 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
18 |
19 | #endif
20 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/string.h:
--------------------------------------------------------------------------------
1 | #ifndef _STRING_H_
2 | #define _STRING_H_
3 |
4 | #ifndef NULL
5 | #define NULL ((void *) 0)
6 | #endif
7 |
8 | #ifndef _SIZE_T
9 | #define _SIZE_T
10 | typedef unsigned int size_t;
11 | #endif
12 |
13 | extern char * strerror(int errno);
14 |
15 | /*
16 | * This string-include defines all string functions as inline
17 | * functions. Use gcc. It also assumes ds=es=data space, this should be
18 | * normal. Most of the string-functions are rather heavily hand-optimized,
19 | * see especially strtok,strstr,str[c]spn. They should work, but are not
20 | * very easy to understand. Everything is done entirely within the register
21 | * set, making the functions fast and clean. String instructions have been
22 | * used through-out, making for "slightly" unclear code :-)
23 | *
24 | * (C) 1991 Linus Torvalds
25 | */
26 |
27 | extern inline char * strcpy(char * dest,const char *src);
28 | extern inline char * strcat(char * dest,const char * src);
29 | extern inline int strcmp(const char * cs,const char * ct);
30 | extern inline int strspn(const char * cs, const char * ct);
31 | extern inline int strcspn(const char * cs, const char * ct);
32 | extern inline char * strpbrk(const char * cs,const char * ct);
33 | extern inline char * strstr(const char * cs,const char * ct);
34 | extern inline int strlen(const char * s);
35 | extern char * ___strtok;
36 |
37 | extern inline char * strtok(char * s,const char * ct);
38 |
39 | /*
40 | * Changes by falcon, the original return value is static
41 | * inline ... it can not be called by other functions in another files.
42 | */
43 |
44 | extern inline void * memcpy(void * dest,const void * src, int n);
45 | extern inline void * memmove(void * dest,const void * src, int n);
46 | extern inline void * memchr(const void * cs,char c,int count);
47 | #endif
48 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/sys/stat.h:
--------------------------------------------------------------------------------
1 | #ifndef _SYS_STAT_H
2 | #define _SYS_STAT_H
3 |
4 | #include
5 |
6 | struct stat {
7 | dev_t st_dev;
8 | ino_t st_ino;
9 | umode_t st_mode;
10 | nlink_t st_nlink;
11 | uid_t st_uid;
12 | gid_t st_gid;
13 | dev_t st_rdev;
14 | off_t st_size;
15 | time_t st_atime;
16 | time_t st_mtime;
17 | time_t st_ctime;
18 | };
19 |
20 | #define S_IFMT 00170000
21 | #define S_IFREG 0100000
22 | #define S_IFBLK 0060000
23 | #define S_IFDIR 0040000
24 | #define S_IFCHR 0020000
25 | #define S_IFIFO 0010000
26 | #define S_ISUID 0004000
27 | #define S_ISGID 0002000
28 | #define S_ISVTX 0001000
29 |
30 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
31 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
32 | #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
33 | #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
34 | #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
35 |
36 | #define S_IRWXU 00700
37 | #define S_IRUSR 00400
38 | #define S_IWUSR 00200
39 | #define S_IXUSR 00100
40 |
41 | #define S_IRWXG 00070
42 | #define S_IRGRP 00040
43 | #define S_IWGRP 00020
44 | #define S_IXGRP 00010
45 |
46 | #define S_IRWXO 00007
47 | #define S_IROTH 00004
48 | #define S_IWOTH 00002
49 | #define S_IXOTH 00001
50 |
51 | extern int chmod(const char *_path, mode_t mode);
52 | extern int fstat(int fildes, struct stat *stat_buf);
53 | extern int mkdir(const char *_path, mode_t mode);
54 | extern int mkfifo(const char *_path, mode_t mode);
55 | extern int stat(const char *filename, struct stat *stat_buf);
56 | extern mode_t umask(mode_t mask);
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/sys/times.h:
--------------------------------------------------------------------------------
1 | #ifndef _TIMES_H
2 | #define _TIMES_H
3 |
4 | #include
5 |
6 | struct tms {
7 | time_t tms_utime;
8 | time_t tms_stime;
9 | time_t tms_cutime;
10 | time_t tms_cstime;
11 | };
12 |
13 | extern time_t times(struct tms * tp);
14 |
15 | #endif
16 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/sys/types.h:
--------------------------------------------------------------------------------
1 | #ifndef _SYS_TYPES_H
2 | #define _SYS_TYPES_H
3 |
4 | #ifndef _SIZE_T
5 | #define _SIZE_T
6 | typedef unsigned int size_t;
7 | #endif
8 |
9 | #ifndef _TIME_T
10 | #define _TIME_T
11 | typedef long time_t;
12 | #endif
13 |
14 | #ifndef _PTRDIFF_T
15 | #define _PTRDIFF_T
16 | typedef long ptrdiff_t;
17 | #endif
18 |
19 | #ifndef NULL
20 | #define NULL ((void *) 0)
21 | #endif
22 |
23 | typedef int pid_t;
24 | typedef unsigned short uid_t;
25 | typedef unsigned char gid_t;
26 | typedef unsigned short dev_t;
27 | typedef unsigned short ino_t;
28 | typedef unsigned short mode_t;
29 | typedef unsigned short umode_t;
30 | typedef unsigned char nlink_t;
31 | typedef int daddr_t;
32 | typedef long off_t;
33 | typedef unsigned char u_char;
34 | typedef unsigned short ushort;
35 |
36 | typedef struct { int quot,rem; } div_t;
37 | typedef struct { long quot,rem; } ldiv_t;
38 |
39 | struct ustat {
40 | daddr_t f_tfree;
41 | ino_t f_tinode;
42 | char f_fname[6];
43 | char f_fpack[6];
44 | };
45 |
46 | #endif
47 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/sys/utsname.h:
--------------------------------------------------------------------------------
1 | #ifndef _SYS_UTSNAME_H
2 | #define _SYS_UTSNAME_H
3 |
4 | #include
5 |
6 | struct utsname {
7 | char sysname[9];
8 | char nodename[9];
9 | char release[9];
10 | char version[9];
11 | char machine[9];
12 | };
13 |
14 | extern int uname(struct utsname * utsbuf);
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/sys/wait.h:
--------------------------------------------------------------------------------
1 | #ifndef _SYS_WAIT_H
2 | #define _SYS_WAIT_H
3 |
4 | #include
5 |
6 | #define _LOW(v) ( (v) & 0377)
7 | #define _HIGH(v) ( ((v) >> 8) & 0377)
8 |
9 | /* options for waitpid, WUNTRACED not supported */
10 | #define WNOHANG 1
11 | #define WUNTRACED 2
12 |
13 | #define WIFEXITED(s) (!((s)&0xFF)
14 | #define WIFSTOPPED(s) (((s)&0xFF)==0x7F)
15 | #define WEXITSTATUS(s) (((s)>>8)&0xFF)
16 | #define WTERMSIG(s) ((s)&0x7F)
17 | #define WSTOPSIG(s) (((s)>>8)&0xFF)
18 | #define WIFSIGNALED(s) (((unsigned int)(s)-1 & 0xFFFF) < 0xFF)
19 |
20 | pid_t wait(int *stat_loc);
21 | pid_t waitpid(pid_t pid, int *stat_loc, int options);
22 |
23 | #endif
24 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/termios.h:
--------------------------------------------------------------------------------
1 | #ifndef _TERMIOS_H
2 | #define _TERMIOS_H
3 |
4 | #define TTY_BUF_SIZE 1024
5 |
6 | /* 0x54 is just a magic number to make these relatively uniqe ('T') */
7 |
8 | #define TCGETS 0x5401
9 | #define TCSETS 0x5402
10 | #define TCSETSW 0x5403
11 | #define TCSETSF 0x5404
12 | #define TCGETA 0x5405
13 | #define TCSETA 0x5406
14 | #define TCSETAW 0x5407
15 | #define TCSETAF 0x5408
16 | #define TCSBRK 0x5409
17 | #define TCXONC 0x540A
18 | #define TCFLSH 0x540B
19 | #define TIOCEXCL 0x540C
20 | #define TIOCNXCL 0x540D
21 | #define TIOCSCTTY 0x540E
22 | #define TIOCGPGRP 0x540F
23 | #define TIOCSPGRP 0x5410
24 | #define TIOCOUTQ 0x5411
25 | #define TIOCSTI 0x5412
26 | #define TIOCGWINSZ 0x5413
27 | #define TIOCSWINSZ 0x5414
28 | #define TIOCMGET 0x5415
29 | #define TIOCMBIS 0x5416
30 | #define TIOCMBIC 0x5417
31 | #define TIOCMSET 0x5418
32 | #define TIOCGSOFTCAR 0x5419
33 | #define TIOCSSOFTCAR 0x541A
34 | #define TIOCINQ 0x541B
35 |
36 | struct winsize {
37 | unsigned short ws_row;
38 | unsigned short ws_col;
39 | unsigned short ws_xpixel;
40 | unsigned short ws_ypixel;
41 | };
42 |
43 | #define NCC 8
44 | struct termio {
45 | unsigned short c_iflag; /* input mode flags */
46 | unsigned short c_oflag; /* output mode flags */
47 | unsigned short c_cflag; /* control mode flags */
48 | unsigned short c_lflag; /* local mode flags */
49 | unsigned char c_line; /* line discipline */
50 | unsigned char c_cc[NCC]; /* control characters */
51 | };
52 |
53 | #define NCCS 17
54 | struct termios {
55 | unsigned long c_iflag; /* input mode flags */
56 | unsigned long c_oflag; /* output mode flags */
57 | unsigned long c_cflag; /* control mode flags */
58 | unsigned long c_lflag; /* local mode flags */
59 | unsigned char c_line; /* line discipline */
60 | unsigned char c_cc[NCCS]; /* control characters */
61 | };
62 |
63 | /* c_cc characters */
64 | #define VINTR 0
65 | #define VQUIT 1
66 | #define VERASE 2
67 | #define VKILL 3
68 | #define VEOF 4
69 | #define VTIME 5
70 | #define VMIN 6
71 | #define VSWTC 7
72 | #define VSTART 8
73 | #define VSTOP 9
74 | #define VSUSP 10
75 | #define VEOL 11
76 | #define VREPRINT 12
77 | #define VDISCARD 13
78 | #define VWERASE 14
79 | #define VLNEXT 15
80 | #define VEOL2 16
81 |
82 | /* c_iflag bits */
83 | #define IGNBRK 0000001
84 | #define BRKINT 0000002
85 | #define IGNPAR 0000004
86 | #define PARMRK 0000010
87 | #define INPCK 0000020
88 | #define ISTRIP 0000040
89 | #define INLCR 0000100
90 | #define IGNCR 0000200
91 | #define ICRNL 0000400
92 | #define IUCLC 0001000
93 | #define IXON 0002000
94 | #define IXANY 0004000
95 | #define IXOFF 0010000
96 | #define IMAXBEL 0020000
97 |
98 | /* c_oflag bits */
99 | #define OPOST 0000001
100 | #define OLCUC 0000002
101 | #define ONLCR 0000004
102 | #define OCRNL 0000010
103 | #define ONOCR 0000020
104 | #define ONLRET 0000040
105 | #define OFILL 0000100
106 | #define OFDEL 0000200
107 | #define NLDLY 0000400
108 | #define NL0 0000000
109 | #define NL1 0000400
110 | #define CRDLY 0003000
111 | #define CR0 0000000
112 | #define CR1 0001000
113 | #define CR2 0002000
114 | #define CR3 0003000
115 | #define TABDLY 0014000
116 | #define TAB0 0000000
117 | #define TAB1 0004000
118 | #define TAB2 0010000
119 | #define TAB3 0014000
120 | #define XTABS 0014000
121 | #define BSDLY 0020000
122 | #define BS0 0000000
123 | #define BS1 0020000
124 | #define VTDLY 0040000
125 | #define VT0 0000000
126 | #define VT1 0040000
127 | #define FFDLY 0040000
128 | #define FF0 0000000
129 | #define FF1 0040000
130 |
131 | /* c_cflag bit meaning */
132 | #define CBAUD 0000017
133 | #define B0 0000000 /* hang up */
134 | #define B50 0000001
135 | #define B75 0000002
136 | #define B110 0000003
137 | #define B134 0000004
138 | #define B150 0000005
139 | #define B200 0000006
140 | #define B300 0000007
141 | #define B600 0000010
142 | #define B1200 0000011
143 | #define B1800 0000012
144 | #define B2400 0000013
145 | #define B4800 0000014
146 | #define B9600 0000015
147 | #define B19200 0000016
148 | #define B38400 0000017
149 | #define EXTA B19200
150 | #define EXTB B38400
151 | #define CSIZE 0000060
152 | #define CS5 0000000
153 | #define CS6 0000020
154 | #define CS7 0000040
155 | #define CS8 0000060
156 | #define CSTOPB 0000100
157 | #define CREAD 0000200
158 | #define CPARENB 0000400
159 | #define CPARODD 0001000
160 | #define HUPCL 0002000
161 | #define CLOCAL 0004000
162 | #define CIBAUD 03600000 /* input baud rate (not used) */
163 | #define CRTSCTS 020000000000 /* flow control */
164 |
165 | #define PARENB CPARENB
166 | #define PARODD CPARODD
167 |
168 | /* c_lflag bits */
169 | #define ISIG 0000001
170 | #define ICANON 0000002
171 | #define XCASE 0000004
172 | #define ECHO 0000010
173 | #define ECHOE 0000020
174 | #define ECHOK 0000040
175 | #define ECHONL 0000100
176 | #define NOFLSH 0000200
177 | #define TOSTOP 0000400
178 | #define ECHOCTL 0001000
179 | #define ECHOPRT 0002000
180 | #define ECHOKE 0004000
181 | #define FLUSHO 0010000
182 | #define PENDIN 0040000
183 | #define IEXTEN 0100000
184 |
185 | /* modem lines */
186 | #define TIOCM_LE 0x001
187 | #define TIOCM_DTR 0x002
188 | #define TIOCM_RTS 0x004
189 | #define TIOCM_ST 0x008
190 | #define TIOCM_SR 0x010
191 | #define TIOCM_CTS 0x020
192 | #define TIOCM_CAR 0x040
193 | #define TIOCM_RNG 0x080
194 | #define TIOCM_DSR 0x100
195 | #define TIOCM_CD TIOCM_CAR
196 | #define TIOCM_RI TIOCM_RNG
197 |
198 | /* tcflow() and TCXONC use these */
199 | #define TCOOFF 0
200 | #define TCOON 1
201 | #define TCIOFF 2
202 | #define TCION 3
203 |
204 | /* tcflush() and TCFLSH use these */
205 | #define TCIFLUSH 0
206 | #define TCOFLUSH 1
207 | #define TCIOFLUSH 2
208 |
209 | /* tcsetattr uses these */
210 | #define TCSANOW 0
211 | #define TCSADRAIN 1
212 | #define TCSAFLUSH 2
213 |
214 | typedef int speed_t;
215 |
216 | extern speed_t cfgetispeed(struct termios *termios_p);
217 | extern speed_t cfgetospeed(struct termios *termios_p);
218 | extern int cfsetispeed(struct termios *termios_p, speed_t speed);
219 | extern int cfsetospeed(struct termios *termios_p, speed_t speed);
220 | extern int tcdrain(int fildes);
221 | extern int tcflow(int fildes, int action);
222 | extern int tcflush(int fildes, int queue_selector);
223 | extern int tcgetattr(int fildes, struct termios *termios_p);
224 | extern int tcsendbreak(int fildes, int duration);
225 | extern int tcsetattr(int fildes, int optional_actions,
226 | struct termios *termios_p);
227 |
228 | #endif
229 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/time.h:
--------------------------------------------------------------------------------
1 | #ifndef _TIME_H
2 | #define _TIME_H
3 |
4 | #ifndef _TIME_T
5 | #define _TIME_T
6 | typedef long time_t;
7 | #endif
8 |
9 | #ifndef _SIZE_T
10 | #define _SIZE_T
11 | typedef unsigned int size_t;
12 | #endif
13 |
14 | #define CLOCKS_PER_SEC 100
15 |
16 | typedef long clock_t;
17 |
18 | struct tm {
19 | int tm_sec;
20 | int tm_min;
21 | int tm_hour;
22 | int tm_mday;
23 | int tm_mon;
24 | int tm_year;
25 | int tm_wday;
26 | int tm_yday;
27 | int tm_isdst;
28 | };
29 |
30 | clock_t clock(void);
31 | time_t time(time_t * tp);
32 | double difftime(time_t time2, time_t time1);
33 | time_t mktime(struct tm * tp);
34 |
35 | char * asctime(const struct tm * tp);
36 | char * ctime(const time_t * tp);
37 | struct tm * gmtime(const time_t *tp);
38 | struct tm *localtime(const time_t * tp);
39 | size_t strftime(char * s, size_t smax, const char * fmt, const struct tm * tp);
40 | void tzset(void);
41 |
42 | #endif
43 |
--------------------------------------------------------------------------------
/lab_2/release_HW/include/utime.h:
--------------------------------------------------------------------------------
1 | #ifndef _UTIME_H
2 | #define _UTIME_H
3 |
4 | #include /* I know - shouldn't do this, but .. */
5 |
6 | struct utimbuf {
7 | time_t actime;
8 | time_t modtime;
9 | };
10 |
11 | extern int utime(const char *filename, struct utimbuf *times);
12 |
13 | #endif
14 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # Makefile for the FREAX-kernel.
3 | #
4 | # Note! Dependencies are done automagically by 'make dep', which also
5 | # removes any old dependencies. DON'T put your own dependencies here
6 | # unless it's something special (ie not a .c file).
7 | #
8 |
9 | include ../Makefile.header
10 |
11 | LDFLAGS += -r
12 |
13 | # NOTE!: do not try to add -On to CFLAGS with gcc4.x, which will optimize the
14 | # memcpy function a lot to let the kernel not work, for fixing this problem,
15 | # please refer to glibc and rewrite the memcpy in include/string.h, or just not
16 | # use any -On options with gcc 4.x when compiling it. in fact, we only can not
17 | # use -On with gcc4.x to compile fork.c, but after that you need to isolate the
18 | # compling procedure of it, it's not good too. for playing with linux-0.11, we
19 | # just have fun, have no much concerning about the performance.
20 |
21 | CFLAGS += -I../include
22 |
23 | CPP += -I../include
24 |
25 | .c.s:
26 | @$(CC) $(CFLAGS) \
27 | -S -o $*.s $<
28 | .s.o:
29 | @$(AS) -o $*.o $<
30 | .c.o:
31 | @$(CC) $(CFLAGS) \
32 | -c -o $*.o $<
33 |
34 | OBJS = sched.o system_call.o traps.o asm.o fork.o \
35 | panic.o printk.o vsprintf.o sys.o exit.o \
36 | signal.o mktime.o print_str2num.o
37 |
38 | kernel.o: $(OBJS)
39 | @$(LD) $(LDFLAGS) -o kernel.o $(OBJS)
40 | @sync
41 |
42 | clean:
43 | @rm -f core *.o *.a tmp_make keyboard.s
44 | @for i in *.c;do rm -f `basename $$i .c`.s;done
45 | @for i in chr_drv blk_drv math; do make clean -C $$i; done
46 |
47 | dep:
48 | @sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
49 | @(for i in *.c;do echo -n `echo $$i | sed 's,\.c,\.s,'`" "; \
50 | $(CPP) -M $$i;done) >> tmp_make
51 | @cp tmp_make Makefile
52 | @for i in chr_drv blk_drv; do make dep -C $$i; done
53 |
54 | ### Dependencies:
55 | exit.s exit.o: exit.c ../include/errno.h ../include/signal.h \
56 | ../include/sys/types.h ../include/sys/wait.h ../include/linux/sched.h \
57 | ../include/linux/head.h ../include/linux/fs.h ../include/linux/mm.h \
58 | ../include/linux/kernel.h ../include/linux/tty.h ../include/termios.h \
59 | ../include/asm/segment.h
60 | fork.s fork.o: fork.c ../include/errno.h ../include/linux/sched.h \
61 | ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
62 | ../include/linux/mm.h ../include/signal.h ../include/linux/kernel.h \
63 | ../include/asm/segment.h ../include/asm/system.h
64 | mktime.s mktime.o: mktime.c ../include/time.h
65 | panic.s panic.o: panic.c ../include/linux/kernel.h ../include/linux/sched.h \
66 | ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
67 | ../include/linux/mm.h ../include/signal.h
68 | printk.s printk.o: printk.c ../include/stdarg.h ../include/stddef.h \
69 | ../include/linux/kernel.h
70 | sched.s sched.o: sched.c ../include/linux/sched.h ../include/linux/head.h \
71 | ../include/linux/fs.h ../include/sys/types.h ../include/linux/mm.h \
72 | ../include/signal.h ../include/linux/kernel.h ../include/linux/sys.h \
73 | ../include/linux/fdreg.h ../include/asm/system.h ../include/asm/io.h \
74 | ../include/asm/segment.h
75 | signal.s signal.o: signal.c ../include/linux/sched.h ../include/linux/head.h \
76 | ../include/linux/fs.h ../include/sys/types.h ../include/linux/mm.h \
77 | ../include/signal.h ../include/linux/kernel.h ../include/asm/segment.h
78 | sys.s sys.o: sys.c ../include/errno.h ../include/linux/sched.h \
79 | ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
80 | ../include/linux/mm.h ../include/signal.h ../include/linux/tty.h \
81 | ../include/termios.h ../include/linux/kernel.h ../include/asm/segment.h \
82 | ../include/sys/times.h ../include/sys/utsname.h
83 | traps.s traps.o: traps.c ../include/string.h ../include/linux/head.h \
84 | ../include/linux/sched.h ../include/linux/fs.h ../include/sys/types.h \
85 | ../include/linux/mm.h ../include/signal.h ../include/linux/kernel.h \
86 | ../include/asm/system.h ../include/asm/segment.h ../include/asm/io.h
87 | vsprintf.s vsprintf.o: vsprintf.c ../include/stdarg.h ../include/string.h
88 | print_str2num.s print_str2num.o: print_str2num.c ../include/asm/segment.h
89 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/asm.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/asm.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/asm.s:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/asm.s
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * asm.s contains the low-level code for most hardware faults.
9 | * page_exception is handled by the mm, so that isn't here. This
10 | * file also handles (hopefully) fpu-exceptions due to TS-bit, as
11 | * the fpu must be properly saved/resored. This hasn't been tested.
12 | */
13 |
14 | .globl divide_error,debug,nmi,int3,overflow,bounds,invalid_op
15 | .globl double_fault,coprocessor_segment_overrun
16 | .globl invalid_TSS,segment_not_present,stack_segment
17 | .globl general_protection,coprocessor_error,irq13,reserved
18 |
19 | divide_error:
20 | pushl $do_divide_error
21 | no_error_code:
22 | xchgl %eax,(%esp)
23 | pushl %ebx
24 | pushl %ecx
25 | pushl %edx
26 | pushl %edi
27 | pushl %esi
28 | pushl %ebp
29 | push %ds
30 | push %es
31 | push %fs
32 | pushl $0 # "error code"
33 | lea 44(%esp),%edx
34 | pushl %edx
35 | movl $0x10,%edx
36 | mov %dx,%ds
37 | mov %dx,%es
38 | mov %dx,%fs
39 | call *%eax
40 | addl $8,%esp
41 | pop %fs
42 | pop %es
43 | pop %ds
44 | popl %ebp
45 | popl %esi
46 | popl %edi
47 | popl %edx
48 | popl %ecx
49 | popl %ebx
50 | popl %eax
51 | iret
52 |
53 | debug:
54 | pushl $do_int3 # _do_debug
55 | jmp no_error_code
56 |
57 | nmi:
58 | pushl $do_nmi
59 | jmp no_error_code
60 |
61 | int3:
62 | pushl $do_int3
63 | jmp no_error_code
64 |
65 | overflow:
66 | pushl $do_overflow
67 | jmp no_error_code
68 |
69 | bounds:
70 | pushl $do_bounds
71 | jmp no_error_code
72 |
73 | invalid_op:
74 | pushl $do_invalid_op
75 | jmp no_error_code
76 |
77 | coprocessor_segment_overrun:
78 | pushl $do_coprocessor_segment_overrun
79 | jmp no_error_code
80 |
81 | reserved:
82 | pushl $do_reserved
83 | jmp no_error_code
84 |
85 | irq13:
86 | pushl %eax
87 | xorb %al,%al
88 | outb %al,$0xF0
89 | movb $0x20,%al
90 | outb %al,$0x20
91 | jmp 1f
92 | 1: jmp 1f
93 | 1: outb %al,$0xA0
94 | popl %eax
95 | jmp coprocessor_error
96 |
97 | double_fault:
98 | pushl $do_double_fault
99 | error_code:
100 | xchgl %eax,4(%esp) # error code <-> %eax
101 | xchgl %ebx,(%esp) # &function <-> %ebx
102 | pushl %ecx
103 | pushl %edx
104 | pushl %edi
105 | pushl %esi
106 | pushl %ebp
107 | push %ds
108 | push %es
109 | push %fs
110 | pushl %eax # error code
111 | lea 44(%esp),%eax # offset
112 | pushl %eax
113 | movl $0x10,%eax
114 | mov %ax,%ds
115 | mov %ax,%es
116 | mov %ax,%fs
117 | call *%ebx
118 | addl $8,%esp
119 | pop %fs
120 | pop %es
121 | pop %ds
122 | popl %ebp
123 | popl %esi
124 | popl %edi
125 | popl %edx
126 | popl %ecx
127 | popl %ebx
128 | popl %eax
129 | iret
130 |
131 | invalid_TSS:
132 | pushl $do_invalid_TSS
133 | jmp error_code
134 |
135 | segment_not_present:
136 | pushl $do_segment_not_present
137 | jmp error_code
138 |
139 | stack_segment:
140 | pushl $do_stack_segment
141 | jmp error_code
142 |
143 | general_protection:
144 | pushl $do_general_protection
145 | jmp error_code
146 |
147 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # Makefile for the FREAX-kernel block device drivers.
3 | #
4 | # Note! Dependencies are done automagically by 'make dep', which also
5 | # removes any old dependencies. DON'T put your own dependencies here
6 | # unless it's something special (ie not a .c file).
7 | #
8 |
9 | include ../../Makefile.header
10 |
11 | CFLAGS += -I../../include
12 | CPP += -I../../include
13 |
14 | .c.s:
15 | @$(CC) $(CFLAGS) \
16 | -S -o $*.s $<
17 | .s.o:
18 | @$(AS) -o $*.o $<
19 | .c.o:
20 | @$(CC) $(CFLAGS) \
21 | -c -o $*.o $<
22 |
23 | OBJS = ll_rw_blk.o floppy.o hd.o ramdisk.o
24 |
25 | blk_drv.a: $(OBJS)
26 | @$(AR) rcs blk_drv.a $(OBJS)
27 | @sync
28 |
29 | clean:
30 | @rm -f core *.o *.a tmp_make
31 | @for i in *.c;do rm -f `basename $$i .c`.s;done
32 |
33 | dep:
34 | @sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
35 | @(for i in *.c;do echo -n `echo $$i | sed 's,\.c,\.s,'`" "; \
36 | $(CPP) -M $$i;done) >> tmp_make
37 | @cp tmp_make Makefile
38 |
39 | ### Dependencies:
40 | floppy.s floppy.o: floppy.c ../../include/linux/sched.h ../../include/linux/head.h \
41 | ../../include/linux/fs.h ../../include/sys/types.h \
42 | ../../include/linux/mm.h ../../include/signal.h \
43 | ../../include/linux/kernel.h ../../include/linux/fdreg.h \
44 | ../../include/asm/system.h ../../include/asm/io.h \
45 | ../../include/asm/segment.h blk.h
46 | hd.s hd.o: hd.c ../../include/linux/config.h ../../include/linux/sched.h \
47 | ../../include/linux/head.h ../../include/linux/fs.h \
48 | ../../include/sys/types.h ../../include/linux/mm.h \
49 | ../../include/signal.h ../../include/linux/kernel.h \
50 | ../../include/linux/hdreg.h ../../include/asm/system.h \
51 | ../../include/asm/io.h ../../include/asm/segment.h blk.h
52 | ll_rw_blk.s ll_rw_blk.o: ll_rw_blk.c ../../include/errno.h \
53 | ../../include/linux/sched.h ../../include/linux/head.h \
54 | ../../include/linux/fs.h ../../include/sys/types.h \
55 | ../../include/linux/mm.h ../../include/signal.h \
56 | ../../include/linux/kernel.h ../../include/asm/system.h blk.h
57 | ramdisk.s ramdisk.o: ramdisk.c ../../include/string.h ../../include/linux/config.h \
58 | ../../include/linux/sched.h ../../include/linux/head.h \
59 | ../../include/linux/fs.h ../../include/sys/types.h \
60 | ../../include/linux/mm.h ../../include/signal.h \
61 | ../../include/linux/kernel.h ../../include/asm/system.h \
62 | ../../include/asm/segment.h ../../include/asm/memory.h blk.h
63 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/blk.h:
--------------------------------------------------------------------------------
1 | #ifndef _BLK_H
2 | #define _BLK_H
3 |
4 | #define NR_BLK_DEV 7
5 | /*
6 | * NR_REQUEST is the number of entries in the request-queue.
7 | * NOTE that writes may use only the low 2/3 of these: reads
8 | * take precedence.
9 | *
10 | * 32 seems to be a reasonable number: enough to get some benefit
11 | * from the elevator-mechanism, but not so much as to lock a lot of
12 | * buffers when they are in the queue. 64 seems to be too many (easily
13 | * long pauses in reading when heavy writing/syncing is going on)
14 | */
15 | #define NR_REQUEST 32
16 |
17 | /*
18 | * Ok, this is an expanded form so that we can use the same
19 | * request for paging requests when that is implemented. In
20 | * paging, 'bh' is NULL, and 'waiting' is used to wait for
21 | * read/write completion.
22 | */
23 | struct request {
24 | int dev; /* -1 if no request */
25 | int cmd; /* READ or WRITE */
26 | int errors;
27 | unsigned long sector;
28 | unsigned long nr_sectors;
29 | char * buffer;
30 | struct task_struct * waiting;
31 | struct buffer_head * bh;
32 | struct request * next;
33 | };
34 |
35 | /*
36 | * This is used in the elevator algorithm: Note that
37 | * reads always go before writes. This is natural: reads
38 | * are much more time-critical than writes.
39 | */
40 | #define IN_ORDER(s1,s2) \
41 | ((s1)->cmd<(s2)->cmd || ((s1)->cmd==(s2)->cmd && \
42 | ((s1)->dev < (s2)->dev || ((s1)->dev == (s2)->dev && \
43 | (s1)->sector < (s2)->sector))))
44 |
45 | struct blk_dev_struct {
46 | void (*request_fn)(void);
47 | struct request * current_request;
48 | };
49 |
50 | extern struct blk_dev_struct blk_dev[NR_BLK_DEV];
51 | extern struct request request[NR_REQUEST];
52 | extern struct task_struct * wait_for_request;
53 |
54 | #ifdef MAJOR_NR
55 |
56 | /*
57 | * Add entries as needed. Currently the only block devices
58 | * supported are hard-disks and floppies.
59 | */
60 |
61 | #if (MAJOR_NR == 1)
62 | /* ram disk */
63 | #define DEVICE_NAME "ramdisk"
64 | #define DEVICE_REQUEST do_rd_request
65 | #define DEVICE_NR(device) ((device) & 7)
66 | #define DEVICE_ON(device)
67 | #define DEVICE_OFF(device)
68 |
69 | #elif (MAJOR_NR == 2)
70 | /* floppy */
71 | #define DEVICE_NAME "floppy"
72 | #define DEVICE_INTR do_floppy
73 | #define DEVICE_REQUEST do_fd_request
74 | #define DEVICE_NR(device) ((device) & 3)
75 | #define DEVICE_ON(device) floppy_on(DEVICE_NR(device))
76 | #define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))
77 |
78 | #elif (MAJOR_NR == 3)
79 | /* harddisk */
80 | #define DEVICE_NAME "harddisk"
81 | #define DEVICE_INTR do_hd
82 | #define DEVICE_REQUEST do_hd_request
83 | #define DEVICE_NR(device) (MINOR(device)/5)
84 | #define DEVICE_ON(device)
85 | #define DEVICE_OFF(device)
86 |
87 | #else
88 | /* unknown blk device */
89 | #error "unknown blk device"
90 |
91 | #endif
92 |
93 | #define CURRENT (blk_dev[MAJOR_NR].current_request)
94 | #define CURRENT_DEV DEVICE_NR(CURRENT->dev)
95 |
96 | #ifdef DEVICE_INTR
97 | void (*DEVICE_INTR)(void) = NULL;
98 | #endif
99 | static void (DEVICE_REQUEST)(void);
100 |
101 | static inline void unlock_buffer(struct buffer_head * bh)
102 | {
103 | if (!bh->b_lock)
104 | printk(DEVICE_NAME ": free buffer being unlocked\n");
105 | bh->b_lock=0;
106 | wake_up(&bh->b_wait);
107 | }
108 |
109 | static inline void end_request(int uptodate)
110 | {
111 | DEVICE_OFF(CURRENT->dev);
112 | if (CURRENT->bh) {
113 | CURRENT->bh->b_uptodate = uptodate;
114 | unlock_buffer(CURRENT->bh);
115 | }
116 | if (!uptodate) {
117 | printk(DEVICE_NAME " I/O error\n\r");
118 | printk("dev %04x, block %d\n\r",CURRENT->dev,
119 | CURRENT->bh->b_blocknr);
120 | }
121 | wake_up(&CURRENT->waiting);
122 | wake_up(&wait_for_request);
123 | CURRENT->dev = -1;
124 | CURRENT = CURRENT->next;
125 | }
126 |
127 | #define INIT_REQUEST \
128 | repeat: \
129 | if (!CURRENT) \
130 | return; \
131 | if (MAJOR(CURRENT->dev) != MAJOR_NR) \
132 | panic(DEVICE_NAME ": request list destroyed"); \
133 | if (CURRENT->bh) { \
134 | if (!CURRENT->bh->b_lock) \
135 | panic(DEVICE_NAME ": block not locked"); \
136 | }
137 |
138 | #endif
139 |
140 | #endif
141 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/blk_drv.a:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/blk_drv/blk_drv.a
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/floppy.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/blk_drv/floppy.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/hd.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/blk_drv/hd.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/ll_rw_blk.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/blk_dev/ll_rw.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * This handles all read/write requests to block devices
9 | */
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | #include "blk.h"
16 |
17 | /*
18 | * The request-struct contains all necessary data
19 | * to load a nr of sectors into memory
20 | */
21 | struct request request[NR_REQUEST];
22 |
23 | /*
24 | * used to wait on when there are no free requests
25 | */
26 | struct task_struct * wait_for_request = NULL;
27 |
28 | /* blk_dev_struct is:
29 | * do_request-address
30 | * next-request
31 | */
32 | struct blk_dev_struct blk_dev[NR_BLK_DEV] = {
33 | { NULL, NULL }, /* no_dev */
34 | { NULL, NULL }, /* dev mem */
35 | { NULL, NULL }, /* dev fd */
36 | { NULL, NULL }, /* dev hd */
37 | { NULL, NULL }, /* dev ttyx */
38 | { NULL, NULL }, /* dev tty */
39 | { NULL, NULL } /* dev lp */
40 | };
41 |
42 | static inline void lock_buffer(struct buffer_head * bh)
43 | {
44 | cli();
45 | while (bh->b_lock)
46 | sleep_on(&bh->b_wait);
47 | bh->b_lock=1;
48 | sti();
49 | }
50 |
51 | static inline void unlock_buffer(struct buffer_head * bh)
52 | {
53 | if (!bh->b_lock)
54 | printk("ll_rw_block.c: buffer not locked\n\r");
55 | bh->b_lock = 0;
56 | wake_up(&bh->b_wait);
57 | }
58 |
59 | /*
60 | * add-request adds a request to the linked list.
61 | * It disables interrupts so that it can muck with the
62 | * request-lists in peace.
63 | */
64 | static void add_request(struct blk_dev_struct * dev, struct request * req)
65 | {
66 | struct request * tmp;
67 |
68 | req->next = NULL;
69 | cli();
70 | if (req->bh)
71 | req->bh->b_dirt = 0;
72 | if (!(tmp = dev->current_request)) {
73 | dev->current_request = req;
74 | sti();
75 | (dev->request_fn)();
76 | return;
77 | }
78 | for ( ; tmp->next ; tmp=tmp->next)
79 | if ((IN_ORDER(tmp,req) ||
80 | !IN_ORDER(tmp,tmp->next)) &&
81 | IN_ORDER(req,tmp->next))
82 | break;
83 | req->next=tmp->next;
84 | tmp->next=req;
85 | sti();
86 | }
87 |
88 | static void make_request(int major,int rw, struct buffer_head * bh)
89 | {
90 | struct request * req;
91 | int rw_ahead;
92 |
93 | /* WRITEA/READA is special case - it is not really needed, so if the */
94 | /* buffer is locked, we just forget about it, else it's a normal read */
95 | if ((rw_ahead = (rw == READA || rw == WRITEA))) {
96 | if (bh->b_lock)
97 | return;
98 | if (rw == READA)
99 | rw = READ;
100 | else
101 | rw = WRITE;
102 | }
103 | if (rw!=READ && rw!=WRITE)
104 | panic("Bad block dev command, must be R/W/RA/WA");
105 | lock_buffer(bh);
106 | if ((rw == WRITE && !bh->b_dirt) || (rw == READ && bh->b_uptodate)) {
107 | unlock_buffer(bh);
108 | return;
109 | }
110 | repeat:
111 | /* we don't allow the write-requests to fill up the queue completely:
112 | * we want some room for reads: they take precedence. The last third
113 | * of the requests are only for reads.
114 | */
115 | if (rw == READ)
116 | req = request+NR_REQUEST;
117 | else
118 | req = request+((NR_REQUEST*2)/3);
119 | /* find an empty request */
120 | while (--req >= request)
121 | if (req->dev<0)
122 | break;
123 | /* if none found, sleep on new requests: check for rw_ahead */
124 | if (req < request) {
125 | if (rw_ahead) {
126 | unlock_buffer(bh);
127 | return;
128 | }
129 | sleep_on(&wait_for_request);
130 | goto repeat;
131 | }
132 | /* fill up the request-info, and add it to the queue */
133 | req->dev = bh->b_dev;
134 | req->cmd = rw;
135 | req->errors=0;
136 | req->sector = bh->b_blocknr<<1;
137 | req->nr_sectors = 2;
138 | req->buffer = bh->b_data;
139 | req->waiting = NULL;
140 | req->bh = bh;
141 | req->next = NULL;
142 | add_request(major+blk_dev,req);
143 | }
144 |
145 | void ll_rw_block(int rw, struct buffer_head * bh)
146 | {
147 | unsigned int major;
148 |
149 | if ((major=MAJOR(bh->b_dev)) >= NR_BLK_DEV ||
150 | !(blk_dev[major].request_fn)) {
151 | printk("Trying to read nonexistent block-device\n\r");
152 | return;
153 | }
154 | make_request(major,rw,bh);
155 | }
156 |
157 | void blk_dev_init(void)
158 | {
159 | int i;
160 |
161 | for (i=0 ; i
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | #define MAJOR_NR 1
18 | #include "blk.h"
19 |
20 | char *rd_start;
21 | int rd_length = 0;
22 |
23 | void do_rd_request(void)
24 | {
25 | int len;
26 | char *addr;
27 |
28 | INIT_REQUEST;
29 | addr = rd_start + (CURRENT->sector << 9);
30 | len = CURRENT->nr_sectors << 9;
31 | if ((MINOR(CURRENT->dev) != 1) || (addr+len > rd_start+rd_length)) {
32 | end_request(0);
33 | goto repeat;
34 | }
35 | if (CURRENT-> cmd == WRITE) {
36 | (void ) memcpy(addr,
37 | CURRENT->buffer,
38 | len);
39 | } else if (CURRENT->cmd == READ) {
40 | (void) memcpy(CURRENT->buffer,
41 | addr,
42 | len);
43 | } else
44 | panic("unknown ramdisk-command");
45 | end_request(1);
46 | goto repeat;
47 | }
48 |
49 | /*
50 | * Returns amount of memory which needs to be reserved.
51 | */
52 | long rd_init(long mem_start, int length)
53 | {
54 | int i;
55 | char *cp;
56 |
57 | blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
58 | rd_start = (char *) mem_start;
59 | rd_length = length;
60 | cp = rd_start;
61 | for (i=0; i < length; i++)
62 | *cp++ = '\0';
63 | return(length);
64 | }
65 |
66 | /*
67 | * If the root device is the ram disk, try to load it.
68 | * In order to do this, the root device is originally set to the
69 | * floppy, and we later change it to be ram disk.
70 | */
71 | void rd_load(void)
72 | {
73 | struct buffer_head *bh;
74 | struct super_block s;
75 | int block = 256; /* Start at block 256 */
76 | int i = 1;
77 | int nblocks;
78 | char *cp; /* Move pointer */
79 |
80 | if (!rd_length)
81 | return;
82 | printk("Ram disk: %d bytes, starting at 0x%x\n", rd_length,
83 | (int) rd_start);
84 | if (MAJOR(ROOT_DEV) != 2)
85 | return;
86 | bh = breada(ROOT_DEV,block+1,block,block+2,-1);
87 | if (!bh) {
88 | printk("Disk error while looking for ramdisk!\n");
89 | return;
90 | }
91 | *((struct d_super_block *) &s) = *((struct d_super_block *) bh->b_data);
92 | brelse(bh);
93 | if (s.s_magic != SUPER_MAGIC)
94 | /* No ram disk image present, assume normal floppy boot */
95 | return;
96 | nblocks = s.s_nzones << s.s_log_zone_size;
97 | if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
98 | printk("Ram disk image too big! (%d blocks, %d avail)\n",
99 | nblocks, rd_length >> BLOCK_SIZE_BITS);
100 | return;
101 | }
102 | printk("Loading %d bytes into ram disk... 0000k",
103 | nblocks << BLOCK_SIZE_BITS);
104 | cp = rd_start;
105 | while (nblocks) {
106 | if (nblocks > 2)
107 | bh = breada(ROOT_DEV, block, block+1, block+2, -1);
108 | else
109 | bh = bread(ROOT_DEV, block);
110 | if (!bh) {
111 | printk("I/O error on block %d, aborting load\n",
112 | block);
113 | return;
114 | }
115 | (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
116 | brelse(bh);
117 | printk("\010\010\010\010\010%4dk",i);
118 | cp += BLOCK_SIZE;
119 | block++;
120 | nblocks--;
121 | i++;
122 | }
123 | printk("\010\010\010\010\010done \n");
124 | ROOT_DEV=0x0101;
125 | }
126 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/blk_drv/ramdisk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/blk_drv/ramdisk.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # Makefile for the FREAX-kernel character device drivers.
3 | #
4 | # Note! Dependencies are done automagically by 'make dep', which also
5 | # removes any old dependencies. DON'T put your own dependencies here
6 | # unless it's something special (ie not a .c file).
7 | #
8 |
9 | include ../../Makefile.header
10 |
11 | CFLAGS += -I../../include
12 | CPP += -I../../include
13 |
14 | .c.s:
15 | @$(CC) $(CFLAGS) \
16 | -S -o $*.s $<
17 | .s.o:
18 | @$(AS) -o $*.o $<
19 | .c.o:
20 | @$(CC) $(CFLAGS) \
21 | -c -o $*.o $<
22 |
23 | OBJS = tty_io.o console.o keyboard.o serial.o rs_io.o \
24 | tty_ioctl.o
25 |
26 | chr_drv.a: $(OBJS)
27 | @$(AR) rcs chr_drv.a $(OBJS)
28 | sync
29 |
30 | keyboard.s: kb.S ../../include/linux/config.h
31 | @$(CPP) kb.S -o keyboard.s
32 |
33 | clean:
34 | @rm -f core *.o *.a tmp_make keyboard.s
35 | @for i in *.c;do rm -f `basename $$i .c`.s;done
36 |
37 | dep:
38 | @sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
39 | @(for i in *.c;do echo -n `echo $$i | sed 's,\.c,\.s,'`" "; \
40 | $(CPP) -M $$i;done) >> tmp_make
41 | @cp tmp_make Makefile
42 |
43 | ### Dependencies:
44 | console.s console.o: console.c ../../include/linux/sched.h \
45 | ../../include/linux/head.h ../../include/linux/fs.h \
46 | ../../include/sys/types.h ../../include/linux/mm.h \
47 | ../../include/signal.h ../../include/linux/tty.h \
48 | ../../include/termios.h ../../include/asm/io.h \
49 | ../../include/asm/system.h
50 | serial.s serial.o: serial.c ../../include/linux/tty.h ../../include/termios.h \
51 | ../../include/linux/sched.h ../../include/linux/head.h \
52 | ../../include/linux/fs.h ../../include/sys/types.h \
53 | ../../include/linux/mm.h ../../include/signal.h \
54 | ../../include/asm/system.h ../../include/asm/io.h
55 | tty_io.s tty_io.o: tty_io.c ../../include/ctype.h ../../include/errno.h \
56 | ../../include/signal.h ../../include/sys/types.h \
57 | ../../include/linux/sched.h ../../include/linux/head.h \
58 | ../../include/linux/fs.h ../../include/linux/mm.h \
59 | ../../include/linux/tty.h ../../include/termios.h \
60 | ../../include/asm/segment.h ../../include/asm/system.h
61 | tty_ioctl.s tty_ioctl.o: tty_ioctl.c ../../include/errno.h ../../include/termios.h \
62 | ../../include/linux/sched.h ../../include/linux/head.h \
63 | ../../include/linux/fs.h ../../include/sys/types.h \
64 | ../../include/linux/mm.h ../../include/signal.h \
65 | ../../include/linux/kernel.h ../../include/linux/tty.h \
66 | ../../include/asm/io.h ../../include/asm/segment.h \
67 | ../../include/asm/system.h
68 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/chr_drv.a:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/chr_drv.a
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/console.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/console.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/keyboard.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/keyboard.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/rs_io.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/rs_io.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/rs_io.s:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/rs_io.s
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * rs_io.s
9 | *
10 | * This module implements the rs232 io interrupts.
11 | */
12 |
13 | .text
14 | .globl rs1_interrupt,rs2_interrupt
15 |
16 | size = 1024 /* must be power of two !
17 | and must match the value
18 | in tty_io.c!!! */
19 |
20 | /* these are the offsets into the read/write buffer structures */
21 | rs_addr = 0
22 | head = 4
23 | tail = 8
24 | proc_list = 12
25 | buf = 16
26 |
27 | startup = 256 /* chars left in write queue when we restart it */
28 |
29 | /*
30 | * These are the actual interrupt routines. They look where
31 | * the interrupt is coming from, and take appropriate action.
32 | */
33 | .align 2
34 | rs1_interrupt:
35 | pushl $table_list+8
36 | jmp rs_int
37 | .align 2
38 | rs2_interrupt:
39 | pushl $table_list+16
40 | rs_int:
41 | pushl %edx
42 | pushl %ecx
43 | pushl %ebx
44 | pushl %eax
45 | push %es
46 | push %ds /* as this is an interrupt, we cannot */
47 | pushl $0x10 /* know that bs is ok. Load it */
48 | pop %ds
49 | pushl $0x10
50 | pop %es
51 | movl 24(%esp),%edx
52 | movl (%edx),%edx
53 | movl rs_addr(%edx),%edx
54 | addl $2,%edx /* interrupt ident. reg */
55 | rep_int:
56 | xorl %eax,%eax
57 | inb %dx,%al
58 | testb $1,%al
59 | jne end
60 | cmpb $6,%al /* this shouldn't happen, but ... */
61 | ja end
62 | movl 24(%esp),%ecx
63 | pushl %edx
64 | subl $2,%edx
65 | call *jmp_table(,%eax,2) /* NOTE! not *4, bit0 is 0 already */
66 | popl %edx
67 | jmp rep_int
68 | end: movb $0x20,%al
69 | outb %al,$0x20 /* EOI */
70 | pop %ds
71 | pop %es
72 | popl %eax
73 | popl %ebx
74 | popl %ecx
75 | popl %edx
76 | addl $4,%esp # jump over _table_list entry
77 | iret
78 |
79 | jmp_table:
80 | .long modem_status,write_char,read_char,line_status
81 |
82 | .align 2
83 | modem_status:
84 | addl $6,%edx /* clear intr by reading modem status reg */
85 | inb %dx,%al
86 | ret
87 |
88 | .align 2
89 | line_status:
90 | addl $5,%edx /* clear intr by reading line status reg. */
91 | inb %dx,%al
92 | ret
93 |
94 | .align 2
95 | read_char:
96 | inb %dx,%al
97 | movl %ecx,%edx
98 | subl $table_list,%edx
99 | shrl $3,%edx
100 | movl (%ecx),%ecx # read-queue
101 | movl head(%ecx),%ebx
102 | movb %al,buf(%ecx,%ebx)
103 | incl %ebx
104 | andl $size-1,%ebx
105 | cmpl tail(%ecx),%ebx
106 | je 1f
107 | movl %ebx,head(%ecx)
108 | 1: pushl %edx
109 | call do_tty_interrupt
110 | addl $4,%esp
111 | ret
112 |
113 | .align 2
114 | write_char:
115 | movl 4(%ecx),%ecx # write-queue
116 | movl head(%ecx),%ebx
117 | subl tail(%ecx),%ebx
118 | andl $size-1,%ebx # nr chars in queue
119 | je write_buffer_empty
120 | cmpl $startup,%ebx
121 | ja 1f
122 | movl proc_list(%ecx),%ebx # wake up sleeping process
123 | testl %ebx,%ebx # is there any?
124 | je 1f
125 | movl $0,(%ebx)
126 | 1: movl tail(%ecx),%ebx
127 | movb buf(%ecx,%ebx),%al
128 | outb %al,%dx
129 | incl %ebx
130 | andl $size-1,%ebx
131 | movl %ebx,tail(%ecx)
132 | cmpl head(%ecx),%ebx
133 | je write_buffer_empty
134 | ret
135 | .align 2
136 | write_buffer_empty:
137 | movl proc_list(%ecx),%ebx # wake up sleeping process
138 | testl %ebx,%ebx # is there any?
139 | je 1f
140 | movl $0,(%ebx)
141 | 1: incl %edx
142 | inb %dx,%al
143 | jmp 1f
144 | 1: jmp 1f
145 | 1: andb $0xd,%al /* disable transmit interrupt */
146 | outb %al,%dx
147 | ret
148 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/serial.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/serial.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * serial.c
9 | *
10 | * This module implements the rs232 io functions
11 | * void rs_write(struct tty_struct * queue);
12 | * void rs_init(void);
13 | * and all interrupts pertaining to serial IO.
14 | */
15 |
16 | #include
17 | #include
18 | #include
19 | #include
20 |
21 | #define WAKEUP_CHARS (TTY_BUF_SIZE/4)
22 |
23 | extern void rs1_interrupt(void);
24 | extern void rs2_interrupt(void);
25 |
26 | static void init(int port)
27 | {
28 | outb_p(0x80,port+3); /* set DLAB of line control reg */
29 | outb_p(0x30,port); /* LS of divisor (48 -> 2400 bps */
30 | outb_p(0x00,port+1); /* MS of divisor */
31 | outb_p(0x03,port+3); /* reset DLAB */
32 | outb_p(0x0b,port+4); /* set DTR,RTS, OUT_2 */
33 | outb_p(0x0d,port+1); /* enable all intrs but writes */
34 | (void)inb(port); /* read data port to reset things (?) */
35 | }
36 |
37 | void rs_init(void)
38 | {
39 | set_intr_gate(0x24,rs1_interrupt);
40 | set_intr_gate(0x23,rs2_interrupt);
41 | init(tty_table[1].read_q.data);
42 | init(tty_table[2].read_q.data);
43 | outb(inb_p(0x21)&0xE7,0x21);
44 | }
45 |
46 | /*
47 | * This routine gets called when tty_write has put something into
48 | * the write_queue. It must check wheter the queue is empty, and
49 | * set the interrupt register accordingly
50 | *
51 | * void _rs_write(struct tty_struct * tty);
52 | */
53 | void rs_write(struct tty_struct * tty)
54 | {
55 | cli();
56 | if (!EMPTY(tty->write_q))
57 | outb(inb_p(tty->write_q.data+1)|0x02,tty->write_q.data+1);
58 | sti();
59 | }
60 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/serial.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/serial.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/tty_io.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/tty_io.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/tty_ioctl.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/chr_drv/tty_ioctl.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | #include
8 | #include
9 |
10 | #include
11 | #include
12 | #include
13 |
14 | #include
15 | #include
16 | #include
17 |
18 | static unsigned short quotient[] = {
19 | 0, 2304, 1536, 1047, 857,
20 | 768, 576, 384, 192, 96,
21 | 64, 48, 24, 12, 6, 3
22 | };
23 |
24 | static void change_speed(struct tty_struct * tty)
25 | {
26 | unsigned short port,quot;
27 |
28 | if (!(port = tty->read_q.data))
29 | return;
30 | quot = quotient[tty->termios.c_cflag & CBAUD];
31 | cli();
32 | outb_p(0x80,port+3); /* set DLAB */
33 | outb_p(quot & 0xff,port); /* LS of divisor */
34 | outb_p(quot >> 8,port+1); /* MS of divisor */
35 | outb(0x03,port+3); /* reset DLAB */
36 | sti();
37 | }
38 |
39 | static void flush(struct tty_queue * queue)
40 | {
41 | cli();
42 | queue->head = queue->tail;
43 | sti();
44 | }
45 |
46 | static void wait_until_sent(struct tty_struct * tty)
47 | {
48 | /* do nothing - not implemented */
49 | }
50 |
51 | static void send_break(struct tty_struct * tty)
52 | {
53 | /* do nothing - not implemented */
54 | }
55 |
56 | static int get_termios(struct tty_struct * tty, struct termios * termios)
57 | {
58 | int i;
59 |
60 | verify_area(termios, sizeof (*termios));
61 | for (i=0 ; i< (sizeof (*termios)) ; i++)
62 | put_fs_byte( ((char *)&tty->termios)[i] , i+(char *)termios );
63 | return 0;
64 | }
65 |
66 | static int set_termios(struct tty_struct * tty, struct termios * termios)
67 | {
68 | int i;
69 |
70 | for (i=0 ; i< (sizeof (*termios)) ; i++)
71 | ((char *)&tty->termios)[i]=get_fs_byte(i+(char *)termios);
72 | change_speed(tty);
73 | return 0;
74 | }
75 |
76 | static int get_termio(struct tty_struct * tty, struct termio * termio)
77 | {
78 | int i;
79 | struct termio tmp_termio;
80 |
81 | verify_area(termio, sizeof (*termio));
82 | tmp_termio.c_iflag = tty->termios.c_iflag;
83 | tmp_termio.c_oflag = tty->termios.c_oflag;
84 | tmp_termio.c_cflag = tty->termios.c_cflag;
85 | tmp_termio.c_lflag = tty->termios.c_lflag;
86 | tmp_termio.c_line = tty->termios.c_line;
87 | for(i=0 ; i < NCC ; i++)
88 | tmp_termio.c_cc[i] = tty->termios.c_cc[i];
89 | for (i=0 ; i< (sizeof (*termio)) ; i++)
90 | put_fs_byte( ((char *)&tmp_termio)[i] , i+(char *)termio );
91 | return 0;
92 | }
93 |
94 | /*
95 | * This only works as the 386 is low-byt-first
96 | */
97 | static int set_termio(struct tty_struct * tty, struct termio * termio)
98 | {
99 | int i;
100 | struct termio tmp_termio;
101 |
102 | for (i=0 ; i< (sizeof (*termio)) ; i++)
103 | ((char *)&tmp_termio)[i]=get_fs_byte(i+(char *)termio);
104 | *(unsigned short *)&tty->termios.c_iflag = tmp_termio.c_iflag;
105 | *(unsigned short *)&tty->termios.c_oflag = tmp_termio.c_oflag;
106 | *(unsigned short *)&tty->termios.c_cflag = tmp_termio.c_cflag;
107 | *(unsigned short *)&tty->termios.c_lflag = tmp_termio.c_lflag;
108 | tty->termios.c_line = tmp_termio.c_line;
109 | for(i=0 ; i < NCC ; i++)
110 | tty->termios.c_cc[i] = tmp_termio.c_cc[i];
111 | change_speed(tty);
112 | return 0;
113 | }
114 |
115 | int tty_ioctl(int dev, int cmd, int arg)
116 | {
117 | struct tty_struct * tty;
118 | if (MAJOR(dev) == 5) {
119 | dev=current->tty;
120 | if (dev<0)
121 | panic("tty_ioctl: dev<0");
122 | } else
123 | dev=MINOR(dev);
124 | tty = dev + tty_table;
125 | switch (cmd) {
126 | case TCGETS:
127 | return get_termios(tty,(struct termios *) arg);
128 | case TCSETSF:
129 | flush(&tty->read_q); /* fallthrough */
130 | case TCSETSW:
131 | wait_until_sent(tty); /* fallthrough */
132 | case TCSETS:
133 | return set_termios(tty,(struct termios *) arg);
134 | case TCGETA:
135 | return get_termio(tty,(struct termio *) arg);
136 | case TCSETAF:
137 | flush(&tty->read_q); /* fallthrough */
138 | case TCSETAW:
139 | wait_until_sent(tty); /* fallthrough */
140 | case TCSETA:
141 | return set_termio(tty,(struct termio *) arg);
142 | case TCSBRK:
143 | if (!arg) {
144 | wait_until_sent(tty);
145 | send_break(tty);
146 | }
147 | return 0;
148 | case TCXONC:
149 | return -EINVAL; /* not implemented */
150 | case TCFLSH:
151 | if (arg==0)
152 | flush(&tty->read_q);
153 | else if (arg==1)
154 | flush(&tty->write_q);
155 | else if (arg==2) {
156 | flush(&tty->read_q);
157 | flush(&tty->write_q);
158 | } else
159 | return -EINVAL;
160 | return 0;
161 | case TIOCEXCL:
162 | return -EINVAL; /* not implemented */
163 | case TIOCNXCL:
164 | return -EINVAL; /* not implemented */
165 | case TIOCSCTTY:
166 | return -EINVAL; /* set controlling term NI */
167 | case TIOCGPGRP:
168 | verify_area((void *) arg,4);
169 | put_fs_long(tty->pgrp,(unsigned long *) arg);
170 | return 0;
171 | case TIOCSPGRP:
172 | tty->pgrp=get_fs_long((unsigned long *) arg);
173 | return 0;
174 | case TIOCOUTQ:
175 | verify_area((void *) arg,4);
176 | put_fs_long(CHARS(tty->write_q),(unsigned long *) arg);
177 | return 0;
178 | case TIOCINQ:
179 | verify_area((void *) arg,4);
180 | put_fs_long(CHARS(tty->secondary),
181 | (unsigned long *) arg);
182 | return 0;
183 | case TIOCSTI:
184 | return -EINVAL; /* not implemented */
185 | case TIOCGWINSZ:
186 | return -EINVAL; /* not implemented */
187 | case TIOCSWINSZ:
188 | return -EINVAL; /* not implemented */
189 | case TIOCMGET:
190 | return -EINVAL; /* not implemented */
191 | case TIOCMBIS:
192 | return -EINVAL; /* not implemented */
193 | case TIOCMBIC:
194 | return -EINVAL; /* not implemented */
195 | case TIOCMSET:
196 | return -EINVAL; /* not implemented */
197 | case TIOCGSOFTCAR:
198 | return -EINVAL; /* not implemented */
199 | case TIOCSSOFTCAR:
200 | return -EINVAL; /* not implemented */
201 | default:
202 | return -EINVAL;
203 | }
204 | }
205 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/chr_drv/tty_ioctl.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/chr_drv/tty_ioctl.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/exit.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/exit.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | #include
8 | #include
9 | #include
10 |
11 | #include
12 | #include
13 | #include
14 | #include
15 |
16 | int sys_pause(void);
17 | int sys_close(int fd);
18 |
19 | void release(struct task_struct * p)
20 | {
21 | int i;
22 |
23 | if (!p)
24 | return;
25 | for (i=1 ; i32)
38 | return -EINVAL;
39 | if (priv || (current->euid==p->euid) || suser())
40 | p->signal |= (1<<(sig-1));
41 | else
42 | return -EPERM;
43 | return 0;
44 | }
45 |
46 | static void kill_session(void)
47 | {
48 | struct task_struct **p = NR_TASKS + task;
49 |
50 | while (--p > &FIRST_TASK) {
51 | if (*p && (*p)->session == current->session)
52 | (*p)->signal |= 1<<(SIGHUP-1);
53 | }
54 | }
55 |
56 | /*
57 | * XXX need to check permissions needed to send signals to process
58 | * groups, etc. etc. kill() permissions semantics are tricky!
59 | */
60 | int sys_kill(int pid,int sig)
61 | {
62 | struct task_struct **p = NR_TASKS + task;
63 | int err, retval = 0;
64 |
65 | if (!pid) while (--p > &FIRST_TASK) {
66 | if (*p && (*p)->pgrp == current->pid)
67 | if ((err=send_sig(sig,*p,1)))
68 | retval = err;
69 | } else if (pid>0) while (--p > &FIRST_TASK) {
70 | if (*p && (*p)->pid == pid)
71 | if ((err=send_sig(sig,*p,0)))
72 | retval = err;
73 | } else if (pid == -1) while (--p > &FIRST_TASK) {
74 | if ((err = send_sig(sig,*p,0)))
75 | retval = err;
76 | } else while (--p > &FIRST_TASK)
77 | if (*p && (*p)->pgrp == -pid)
78 | if ((err = send_sig(sig,*p,0)))
79 | retval = err;
80 | return retval;
81 | }
82 |
83 | static void tell_father(int pid)
84 | {
85 | int i;
86 |
87 | if (pid)
88 | for (i=0;ipid != pid)
92 | continue;
93 | task[i]->signal |= (1<<(SIGCHLD-1));
94 | return;
95 | }
96 | /* if we don't find any fathers, we just release ourselves */
97 | /* This is not really OK. Must change it to make father 1 */
98 | printk("BAD BAD - no father found\n\r");
99 | release(current);
100 | }
101 |
102 | int do_exit(long code)
103 | {
104 | int i;
105 | free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));
106 | free_page_tables(get_base(current->ldt[2]),get_limit(0x17));
107 | for (i=0 ; ifather == current->pid) {
109 | task[i]->father = 1;
110 | if (task[i]->state == TASK_ZOMBIE)
111 | /* assumption task[1] is always init */
112 | (void) send_sig(SIGCHLD, task[1], 1);
113 | }
114 | for (i=0 ; ifilp[i])
116 | sys_close(i);
117 | iput(current->pwd);
118 | current->pwd=NULL;
119 | iput(current->root);
120 | current->root=NULL;
121 | iput(current->executable);
122 | current->executable=NULL;
123 | if (current->leader && current->tty >= 0)
124 | tty_table[current->tty].pgrp = 0;
125 | if (last_task_used_math == current)
126 | last_task_used_math = NULL;
127 | if (current->leader)
128 | kill_session();
129 | current->state = TASK_ZOMBIE;
130 | current->exit_code = code;
131 | tell_father(current->father);
132 | schedule();
133 | return (-1); /* just to suppress warnings */
134 | }
135 |
136 | int sys_exit(int error_code)
137 | {
138 | return do_exit((error_code&0xff)<<8);
139 | }
140 |
141 | int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options)
142 | {
143 | int flag, code;
144 | struct task_struct ** p;
145 |
146 | verify_area(stat_addr,4);
147 | repeat:
148 | flag=0;
149 | for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
150 | if (!*p || *p == current)
151 | continue;
152 | if ((*p)->father != current->pid)
153 | continue;
154 | if (pid>0) {
155 | if ((*p)->pid != pid)
156 | continue;
157 | } else if (!pid) {
158 | if ((*p)->pgrp != current->pgrp)
159 | continue;
160 | } else if (pid != -1) {
161 | if ((*p)->pgrp != -pid)
162 | continue;
163 | }
164 | switch ((*p)->state) {
165 | case TASK_STOPPED:
166 | if (!(options & WUNTRACED))
167 | continue;
168 | put_fs_long(0x7f,stat_addr);
169 | return (*p)->pid;
170 | case TASK_ZOMBIE:
171 | current->cutime += (*p)->utime;
172 | current->cstime += (*p)->stime;
173 | flag = (*p)->pid;
174 | code = (*p)->exit_code;
175 | release(*p);
176 | put_fs_long(code,stat_addr);
177 | return flag;
178 | default:
179 | flag=1;
180 | continue;
181 | }
182 | }
183 | if (flag) {
184 | if (options & WNOHANG)
185 | return 0;
186 | current->state=TASK_INTERRUPTIBLE;
187 | schedule();
188 | if (!(current->signal &= ~(1<<(SIGCHLD-1))))
189 | goto repeat;
190 | else
191 | return -EINTR;
192 | }
193 | return -ECHILD;
194 | }
195 |
196 |
197 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/exit.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/exit.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/fork.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/fork.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * 'fork.c' contains the help-routines for the 'fork' system call
9 | * (see also system_call.s), and some misc functions ('verify_area').
10 | * Fork is rather simple, once you get the hang of it, but the memory
11 | * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()'
12 | */
13 | #include
14 | #include
15 |
16 | #include
17 | #include
18 | #include
19 | #include
20 |
21 | extern void write_verify(unsigned long address);
22 |
23 | long last_pid=0;
24 |
25 | void verify_area(void * addr,int size)
26 | {
27 | unsigned long start;
28 |
29 | start = (unsigned long) addr;
30 | size += start & 0xfff;
31 | start &= 0xfffff000;
32 | start += get_base(current->ldt[2]);
33 | while (size>0) {
34 | size -= 4096;
35 | write_verify(start);
36 | start += 4096;
37 | }
38 | }
39 |
40 | int copy_mem(int nr,struct task_struct * p)
41 | {
42 | unsigned long old_data_base,new_data_base,data_limit;
43 | unsigned long old_code_base,new_code_base,code_limit;
44 |
45 | code_limit=get_limit(0x0f);
46 | data_limit=get_limit(0x17);
47 | old_code_base = get_base(current->ldt[1]);
48 | old_data_base = get_base(current->ldt[2]);
49 | if (old_data_base != old_code_base)
50 | panic("We don't support separate I&D");
51 | if (data_limit < code_limit)
52 | panic("Bad data_limit");
53 | new_data_base = new_code_base = nr * 0x4000000;
54 | p->start_code = new_code_base;
55 | set_base(p->ldt[1],new_code_base);
56 | set_base(p->ldt[2],new_data_base);
57 | if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
58 | printk("free_page_tables: from copy_mem\n");
59 | free_page_tables(new_data_base,data_limit);
60 | return -ENOMEM;
61 | }
62 | return 0;
63 | }
64 |
65 | /*
66 | * Ok, this is the main fork-routine. It copies the system process
67 | * information (task[nr]) and sets up the necessary registers. It
68 | * also copies the data segment in it's entirety.
69 | */
70 | int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
71 | long ebx,long ecx,long edx,
72 | long fs,long es,long ds,
73 | long eip,long cs,long eflags,long esp,long ss)
74 | {
75 | struct task_struct *p;
76 | int i;
77 | struct file *f;
78 |
79 | p = (struct task_struct *) get_free_page();
80 | if (!p)
81 | return -EAGAIN;
82 | task[nr] = p;
83 |
84 | // NOTE!: the following statement now work with gcc 4.3.2 now, and you
85 | // must compile _THIS_ memcpy without no -O of gcc.#ifndef GCC4_3
86 | *p = *current; /* NOTE! this doesn't copy the supervisor stack */
87 | p->state = TASK_UNINTERRUPTIBLE;
88 | p->pid = last_pid;
89 | p->father = current->pid;
90 | p->counter = p->priority;
91 | p->signal = 0;
92 | p->alarm = 0;
93 | p->leader = 0; /* process leadership doesn't inherit */
94 | p->utime = p->stime = 0;
95 | p->cutime = p->cstime = 0;
96 | p->start_time = jiffies;
97 | p->tss.back_link = 0;
98 | p->tss.esp0 = PAGE_SIZE + (long) p;
99 | p->tss.ss0 = 0x10;
100 | p->tss.eip = eip;
101 | p->tss.eflags = eflags;
102 | p->tss.eax = 0;
103 | p->tss.ecx = ecx;
104 | p->tss.edx = edx;
105 | p->tss.ebx = ebx;
106 | p->tss.esp = esp;
107 | p->tss.ebp = ebp;
108 | p->tss.esi = esi;
109 | p->tss.edi = edi;
110 | p->tss.es = es & 0xffff;
111 | p->tss.cs = cs & 0xffff;
112 | p->tss.ss = ss & 0xffff;
113 | p->tss.ds = ds & 0xffff;
114 | p->tss.fs = fs & 0xffff;
115 | p->tss.gs = gs & 0xffff;
116 | p->tss.ldt = _LDT(nr);
117 | p->tss.trace_bitmap = 0x80000000;
118 | if (last_task_used_math == current)
119 | __asm__("clts ; fnsave %0"::"m" (p->tss.i387));
120 | if (copy_mem(nr,p)) {
121 | task[nr] = NULL;
122 | free_page((long) p);
123 | return -EAGAIN;
124 | }
125 | for (i=0; ifilp[i]))
127 | f->f_count++;
128 | if (current->pwd)
129 | current->pwd->i_count++;
130 | if (current->root)
131 | current->root->i_count++;
132 | if (current->executable)
133 | current->executable->i_count++;
134 | set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
135 | set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
136 | p->state = TASK_RUNNING; /* do this last, just in case */
137 | return last_pid;
138 | }
139 |
140 | int find_empty_process(void)
141 | {
142 | int i;
143 |
144 | repeat:
145 | if ((++last_pid)<0) last_pid=1;
146 | for(i=0 ; ipid == last_pid) goto repeat;
148 | for(i=1 ; i tmp_make
35 | @(for i in *.c;do echo -n `echo $$i | sed 's,\.c,\.s,'`" "; \
36 | $(CPP) -M $$i;done) >> tmp_make
37 | @cp tmp_make Makefile
38 |
39 | ### Dependencies:
40 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/math/math.a:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/math/math.a
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/math/math_emulate.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/math/math_emulate.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * This directory should contain the math-emulation code.
9 | * Currently only results in a signal.
10 | */
11 |
12 | #include
13 |
14 | #include
15 | #include
16 | #include
17 |
18 | void math_emulate(long edi, long esi, long ebp, long sys_call_ret,
19 | long eax,long ebx,long ecx,long edx,
20 | unsigned short fs,unsigned short es,unsigned short ds,
21 | unsigned long eip,unsigned short cs,unsigned long eflags,
22 | unsigned short ss, unsigned long esp)
23 | {
24 | unsigned char first, second;
25 |
26 | /* 0x0007 means user code space */
27 | if (cs != 0x000F) {
28 | printk("math_emulate: %04x:%08x\n\r",cs,eip);
29 | panic("Math emulation needed in kernel");
30 | }
31 | first = get_fs_byte((char *)((*&eip)++));
32 | second = get_fs_byte((char *)((*&eip)++));
33 | printk("%04x:%08x %02x %02x\n\r",cs,eip-2,first,second);
34 | current->signal |= 1<<(SIGFPE-1);
35 | }
36 |
37 | void math_error(void)
38 | {
39 | __asm__("fnclex");
40 | if (last_task_used_math)
41 | last_task_used_math->signal |= 1<<(SIGFPE-1);
42 | }
43 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/math/math_emulate.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/math/math_emulate.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/mktime.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/mktime.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | #include
8 |
9 | /*
10 | * This isn't the library routine, it is only used in the kernel.
11 | * as such, we don't care about years<1970 etc, but assume everything
12 | * is ok. Similarly, TZ etc is happily ignored. We just do everything
13 | * as easily as possible. Let's find something public for the library
14 | * routines (although I think minix times is public).
15 | */
16 | /*
17 | * PS. I hate whoever though up the year 1970 - couldn't they have gotten
18 | * a leap-year instead? I also hate Gregorius, pope or no. I'm grumpy.
19 | */
20 | #define MINUTE 60
21 | #define HOUR (60*MINUTE)
22 | #define DAY (24*HOUR)
23 | #define YEAR (365*DAY)
24 |
25 | /* interestingly, we assume leap-years */
26 | static int month[12] = {
27 | 0,
28 | DAY*(31),
29 | DAY*(31+29),
30 | DAY*(31+29+31),
31 | DAY*(31+29+31+30),
32 | DAY*(31+29+31+30+31),
33 | DAY*(31+29+31+30+31+30),
34 | DAY*(31+29+31+30+31+30+31),
35 | DAY*(31+29+31+30+31+30+31+31),
36 | DAY*(31+29+31+30+31+30+31+31+30),
37 | DAY*(31+29+31+30+31+30+31+31+30+31),
38 | DAY*(31+29+31+30+31+30+31+31+30+31+30)
39 | };
40 |
41 | long kernel_mktime(struct tm * tm)
42 | {
43 | long res;
44 | int year;
45 | if (tm->tm_year >= 70)
46 | year = tm->tm_year - 70;
47 | else
48 | year = tm->tm_year + 100 -70; /* Y2K bug fix by hellotigercn 20110803 */
49 | /* magic offsets (y+1) needed to get leapyears right.*/
50 | res = YEAR*year + DAY*((year+1)/4);
51 | res += month[tm->tm_mon];
52 | /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
53 | if (tm->tm_mon>1 && ((year+2)%4))
54 | res -= DAY;
55 | res += DAY*(tm->tm_mday-1);
56 | res += HOUR*tm->tm_hour;
57 | res += MINUTE*tm->tm_min;
58 | res += tm->tm_sec;
59 | return res;
60 | }
61 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/mktime.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/mktime.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/panic.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/panic.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * This function is used through-out the kernel (includeinh mm and fs)
9 | * to indicate a major problem.
10 | */
11 | #define PANIC
12 |
13 | #include
14 | #include
15 |
16 | void sys_sync(void); /* it's really int */
17 |
18 | void panic(const char * s)
19 | {
20 | printk("Kernel panic: %s\n\r",s);
21 | if (current == task[0])
22 | printk("In swapper task - not syncing\n\r");
23 | else
24 | sys_sync();
25 | for(;;);
26 | }
27 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/panic.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/panic.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/print_str2num.c:
--------------------------------------------------------------------------------
1 | #define __LIBRARY__
2 | #include
3 | #include
4 |
5 | int sys_print_val(int a)
6 | {
7 | printk("in sys_print_val: %d", a);
8 | }
9 |
10 | int sys_str2num(char *str, int str_len, long *ret)
11 | {
12 | /* *ret = (long)atoi(str); */
13 | long sum = 0;
14 | char temp;
15 | for (int i = 0; i < str_len; i++)
16 | {
17 | temp = get_fs_byte(str + i);
18 | sum = sum * 10 + temp - '0';
19 | }
20 | put_fs_long(sum, ret);
21 | }
22 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/print_str2num.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/print_str2num.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/printk.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/printk.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * When in kernel-mode, we cannot use printf, as fs is liable to
9 | * point to 'interesting' things. Make a printf with fs-saving, and
10 | * all is well.
11 | */
12 | #include
13 | #include
14 |
15 | #include
16 |
17 | static char buf[1024];
18 |
19 | extern int vsprintf(char * buf, const char * fmt, va_list args);
20 |
21 | int printk(const char *fmt, ...)
22 | {
23 | va_list args;
24 | int i;
25 |
26 | va_start(args, fmt);
27 | i=vsprintf(buf,fmt,args);
28 | va_end(args);
29 | __asm__("push %%fs\n\t"
30 | "push %%ds\n\t"
31 | "pop %%fs\n\t"
32 | "pushl %0\n\t"
33 | "pushl $buf\n\t"
34 | "pushl $0\n\t"
35 | "call tty_write\n\t"
36 | "addl $8,%%esp\n\t"
37 | "popl %0\n\t"
38 | "pop %%fs"
39 | ::"r" (i):"ax","cx","dx");
40 | return i;
41 | }
42 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/printk.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/printk.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/sched.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/sched.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/signal.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/signal.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | #include
8 | #include
9 | #include
10 |
11 | #include
12 |
13 | void do_exit(int error_code);
14 |
15 | int sys_sgetmask()
16 | {
17 | return current->blocked;
18 | }
19 |
20 | int sys_ssetmask(int newmask)
21 | {
22 | int old=current->blocked;
23 |
24 | current->blocked = newmask & ~(1<<(SIGKILL-1));
25 | return old;
26 | }
27 |
28 | static inline void save_old(char * from,char * to)
29 | {
30 | int i;
31 |
32 | verify_area(to, sizeof(struct sigaction));
33 | for (i=0 ; i< sizeof(struct sigaction) ; i++) {
34 | put_fs_byte(*from,to);
35 | from++;
36 | to++;
37 | }
38 | }
39 |
40 | static inline void get_new(char * from,char * to)
41 | {
42 | int i;
43 |
44 | for (i=0 ; i< sizeof(struct sigaction) ; i++)
45 | *(to++) = get_fs_byte(from++);
46 | }
47 |
48 | int sys_signal(int signum, long handler, long restorer)
49 | {
50 | struct sigaction tmp;
51 |
52 | if (signum<1 || signum>32 || signum==SIGKILL)
53 | return -1;
54 | tmp.sa_handler = (void (*)(int)) handler;
55 | tmp.sa_mask = 0;
56 | tmp.sa_flags = SA_ONESHOT | SA_NOMASK;
57 | tmp.sa_restorer = (void (*)(void)) restorer;
58 | handler = (long) current->sigaction[signum-1].sa_handler;
59 | current->sigaction[signum-1] = tmp;
60 | return handler;
61 | }
62 |
63 | int sys_sigaction(int signum, const struct sigaction * action,
64 | struct sigaction * oldaction)
65 | {
66 | struct sigaction tmp;
67 |
68 | if (signum<1 || signum>32 || signum==SIGKILL)
69 | return -1;
70 | tmp = current->sigaction[signum-1];
71 | get_new((char *) action,
72 | (char *) (signum-1+current->sigaction));
73 | if (oldaction)
74 | save_old((char *) &tmp,(char *) oldaction);
75 | if (current->sigaction[signum-1].sa_flags & SA_NOMASK)
76 | current->sigaction[signum-1].sa_mask = 0;
77 | else
78 | current->sigaction[signum-1].sa_mask |= (1<<(signum-1));
79 | return 0;
80 | }
81 |
82 | void do_signal(long signr,long eax, long ebx, long ecx, long edx,
83 | long fs, long es, long ds,
84 | long eip, long cs, long eflags,
85 | unsigned long * esp, long ss)
86 | {
87 | unsigned long sa_handler;
88 | long old_eip=eip;
89 | struct sigaction * sa = current->sigaction + signr - 1;
90 | int longs;
91 | unsigned long * tmp_esp;
92 |
93 | sa_handler = (unsigned long) sa->sa_handler;
94 | if (sa_handler==1)
95 | return;
96 | if (!sa_handler) {
97 | if (signr==SIGCHLD)
98 | return;
99 | else
100 | do_exit(1<<(signr-1));
101 | }
102 | if (sa->sa_flags & SA_ONESHOT)
103 | sa->sa_handler = NULL;
104 | *(&eip) = sa_handler;
105 | longs = (sa->sa_flags & SA_NOMASK)?7:8;
106 | *(&esp) -= longs;
107 | verify_area(esp,longs*4);
108 | tmp_esp=esp;
109 | put_fs_long((long) sa->sa_restorer,tmp_esp++);
110 | put_fs_long(signr,tmp_esp++);
111 | if (!(sa->sa_flags & SA_NOMASK))
112 | put_fs_long(current->blocked,tmp_esp++);
113 | put_fs_long(eax,tmp_esp++);
114 | put_fs_long(ecx,tmp_esp++);
115 | put_fs_long(edx,tmp_esp++);
116 | put_fs_long(eflags,tmp_esp++);
117 | put_fs_long(old_eip,tmp_esp++);
118 | current->blocked |= sa->sa_mask;
119 | }
120 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/signal.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/signal.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/sys.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/sys.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | #include
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 |
16 | int sys_ftime()
17 | {
18 | return -ENOSYS;
19 | }
20 |
21 | int sys_break()
22 | {
23 | return -ENOSYS;
24 | }
25 |
26 | int sys_ptrace()
27 | {
28 | return -ENOSYS;
29 | }
30 |
31 | int sys_stty()
32 | {
33 | return -ENOSYS;
34 | }
35 |
36 | int sys_gtty()
37 | {
38 | return -ENOSYS;
39 | }
40 |
41 | int sys_rename()
42 | {
43 | return -ENOSYS;
44 | }
45 |
46 | int sys_prof()
47 | {
48 | return -ENOSYS;
49 | }
50 |
51 | int sys_setregid(int rgid, int egid)
52 | {
53 | if (rgid>0) {
54 | if ((current->gid == rgid) ||
55 | suser())
56 | current->gid = rgid;
57 | else
58 | return(-EPERM);
59 | }
60 | if (egid>0) {
61 | if ((current->gid == egid) ||
62 | (current->egid == egid) ||
63 | (current->sgid == egid) ||
64 | suser())
65 | current->egid = egid;
66 | else
67 | return(-EPERM);
68 | }
69 | return 0;
70 | }
71 |
72 | int sys_setgid(int gid)
73 | {
74 | return(sys_setregid(gid, gid));
75 | }
76 |
77 | int sys_acct()
78 | {
79 | return -ENOSYS;
80 | }
81 |
82 | int sys_phys()
83 | {
84 | return -ENOSYS;
85 | }
86 |
87 | int sys_lock()
88 | {
89 | return -ENOSYS;
90 | }
91 |
92 | int sys_mpx()
93 | {
94 | return -ENOSYS;
95 | }
96 |
97 | int sys_ulimit()
98 | {
99 | return -ENOSYS;
100 | }
101 |
102 | int sys_time(long * tloc)
103 | {
104 | int i;
105 |
106 | i = CURRENT_TIME;
107 | if (tloc) {
108 | verify_area(tloc,4);
109 | put_fs_long(i,(unsigned long *)tloc);
110 | }
111 | return i;
112 | }
113 |
114 | /*
115 | * Unprivileged users may change the real user id to the effective uid
116 | * or vice versa.
117 | */
118 | int sys_setreuid(int ruid, int euid)
119 | {
120 | int old_ruid = current->uid;
121 |
122 | if (ruid>0) {
123 | if ((current->euid==ruid) ||
124 | (old_ruid == ruid) ||
125 | suser())
126 | current->uid = ruid;
127 | else
128 | return(-EPERM);
129 | }
130 | if (euid>0) {
131 | if ((old_ruid == euid) ||
132 | (current->euid == euid) ||
133 | suser())
134 | current->euid = euid;
135 | else {
136 | current->uid = old_ruid;
137 | return(-EPERM);
138 | }
139 | }
140 | return 0;
141 | }
142 |
143 | int sys_setuid(int uid)
144 | {
145 | return(sys_setreuid(uid, uid));
146 | }
147 |
148 | int sys_stime(long * tptr)
149 | {
150 | if (!suser())
151 | return -EPERM;
152 | startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;
153 | return 0;
154 | }
155 |
156 | int sys_times(struct tms * tbuf)
157 | {
158 | if (tbuf) {
159 | verify_area(tbuf,sizeof *tbuf);
160 | put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
161 | put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
162 | put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
163 | put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
164 | }
165 | return jiffies;
166 | }
167 |
168 | int sys_brk(unsigned long end_data_seg)
169 | {
170 | if (end_data_seg >= current->end_code &&
171 | end_data_seg < current->start_stack - 16384)
172 | current->brk = end_data_seg;
173 | return current->brk;
174 | }
175 |
176 | /*
177 | * This needs some heave checking ...
178 | * I just haven't get the stomach for it. I also don't fully
179 | * understand sessions/pgrp etc. Let somebody who does explain it.
180 | */
181 | int sys_setpgid(int pid, int pgid)
182 | {
183 | int i;
184 |
185 | if (!pid)
186 | pid = current->pid;
187 | if (!pgid)
188 | pgid = current->pid;
189 | for (i=0 ; ipid==pid) {
191 | if (task[i]->leader)
192 | return -EPERM;
193 | if (task[i]->session != current->session)
194 | return -EPERM;
195 | task[i]->pgrp = pgid;
196 | return 0;
197 | }
198 | return -ESRCH;
199 | }
200 |
201 | int sys_getpgrp(void)
202 | {
203 | return current->pgrp;
204 | }
205 |
206 | int sys_setsid(void)
207 | {
208 | if (current->leader && !suser())
209 | return -EPERM;
210 | current->leader = 1;
211 | current->session = current->pgrp = current->pid;
212 | current->tty = -1;
213 | return current->pgrp;
214 | }
215 |
216 | int sys_uname(struct utsname * name)
217 | {
218 | static struct utsname thisname = {
219 | "linux .0","nodename","release ","version ","machine "
220 | };
221 | int i;
222 |
223 | if (!name) return -ERROR;
224 | verify_area(name,sizeof *name);
225 | for(i=0;iumask;
233 |
234 | current->umask = mask & 0777;
235 | return (old);
236 | }
237 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/sys.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/sys.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/system_call.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/system_call.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/system_call.s:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/system_call.s
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * system_call.s contains the system-call low-level handling routines.
9 | * This also contains the timer-interrupt handler, as some of the code is
10 | * the same. The hd- and flopppy-interrupts are also here.
11 | *
12 | * NOTE: This code handles signal-recognition, which happens every time
13 | * after a timer-interrupt and after each system call. Ordinary interrupts
14 | * don't handle signal-recognition, as that would clutter them up totally
15 | * unnecessarily.
16 | *
17 | * Stack layout in 'ret_from_system_call':
18 | *
19 | * 0(%esp) - %eax
20 | * 4(%esp) - %ebx
21 | * 8(%esp) - %ecx
22 | * C(%esp) - %edx
23 | * 10(%esp) - %fs
24 | * 14(%esp) - %es
25 | * 18(%esp) - %ds
26 | * 1C(%esp) - %eip
27 | * 20(%esp) - %cs
28 | * 24(%esp) - %eflags
29 | * 28(%esp) - %oldesp
30 | * 2C(%esp) - %oldss
31 | */
32 |
33 | SIG_CHLD = 17
34 |
35 | EAX = 0x00
36 | EBX = 0x04
37 | ECX = 0x08
38 | EDX = 0x0C
39 | FS = 0x10
40 | ES = 0x14
41 | DS = 0x18
42 | EIP = 0x1C
43 | CS = 0x20
44 | EFLAGS = 0x24
45 | OLDESP = 0x28
46 | OLDSS = 0x2C
47 |
48 | state = 0 # these are offsets into the task-struct.
49 | counter = 4
50 | priority = 8
51 | signal = 12
52 | sigaction = 16 # MUST be 16 (=len of sigaction)
53 | blocked = (33*16)
54 |
55 | # offsets within sigaction
56 | sa_handler = 0
57 | sa_mask = 4
58 | sa_flags = 8
59 | sa_restorer = 12
60 |
61 | nr_system_calls = 74
62 |
63 | /*
64 | * Ok, I get parallel printer interrupts while using the floppy for some
65 | * strange reason. Urgel. Now I just ignore them.
66 | */
67 | .globl system_call,sys_fork,timer_interrupt,sys_execve
68 | .globl hd_interrupt,floppy_interrupt,parallel_interrupt
69 | .globl device_not_available, coprocessor_error
70 |
71 | .align 2
72 | bad_sys_call:
73 | movl $-1,%eax
74 | iret
75 | .align 2
76 | reschedule:
77 | pushl $ret_from_sys_call
78 | jmp schedule
79 | .align 2
80 | system_call:
81 | cmpl $nr_system_calls-1,%eax
82 | ja bad_sys_call
83 | push %ds
84 | push %es
85 | push %fs
86 | pushl %edx
87 | pushl %ecx # push %ebx,%ecx,%edx as parameters
88 | pushl %ebx # to the system call
89 | movl $0x10,%edx # set up ds,es to kernel space
90 | mov %dx,%ds
91 | mov %dx,%es
92 | movl $0x17,%edx # fs points to local data space
93 | mov %dx,%fs
94 | call *sys_call_table(,%eax,4)
95 | pushl %eax
96 | movl current,%eax
97 | cmpl $0,state(%eax) # state
98 | jne reschedule
99 | cmpl $0,counter(%eax) # counter
100 | je reschedule
101 | ret_from_sys_call:
102 | movl current,%eax # task[0] cannot have signals
103 | cmpl task,%eax
104 | je 3f
105 | cmpw $0x0f,CS(%esp) # was old code segment supervisor ?
106 | jne 3f
107 | cmpw $0x17,OLDSS(%esp) # was stack segment = 0x17 ?
108 | jne 3f
109 | movl signal(%eax),%ebx
110 | movl blocked(%eax),%ecx
111 | notl %ecx
112 | andl %ebx,%ecx
113 | bsfl %ecx,%ecx
114 | je 3f
115 | btrl %ecx,%ebx
116 | movl %ebx,signal(%eax)
117 | incl %ecx
118 | pushl %ecx
119 | call do_signal
120 | popl %eax
121 | 3: popl %eax
122 | popl %ebx
123 | popl %ecx
124 | popl %edx
125 | pop %fs
126 | pop %es
127 | pop %ds
128 | iret
129 |
130 | .align 2
131 | coprocessor_error:
132 | push %ds
133 | push %es
134 | push %fs
135 | pushl %edx
136 | pushl %ecx
137 | pushl %ebx
138 | pushl %eax
139 | movl $0x10,%eax
140 | mov %ax,%ds
141 | mov %ax,%es
142 | movl $0x17,%eax
143 | mov %ax,%fs
144 | pushl $ret_from_sys_call
145 | jmp math_error
146 |
147 | .align 2
148 | device_not_available:
149 | push %ds
150 | push %es
151 | push %fs
152 | pushl %edx
153 | pushl %ecx
154 | pushl %ebx
155 | pushl %eax
156 | movl $0x10,%eax
157 | mov %ax,%ds
158 | mov %ax,%es
159 | movl $0x17,%eax
160 | mov %ax,%fs
161 | pushl $ret_from_sys_call
162 | clts # clear TS so that we can use math
163 | movl %cr0,%eax
164 | testl $0x4,%eax # EM (math emulation bit)
165 | je math_state_restore
166 | pushl %ebp
167 | pushl %esi
168 | pushl %edi
169 | call math_emulate
170 | popl %edi
171 | popl %esi
172 | popl %ebp
173 | ret
174 |
175 | .align 2
176 | timer_interrupt:
177 | push %ds # save ds,es and put kernel data space
178 | push %es # into them. %fs is used by _system_call
179 | push %fs
180 | pushl %edx # we save %eax,%ecx,%edx as gcc doesn't
181 | pushl %ecx # save those across function calls. %ebx
182 | pushl %ebx # is saved as we use that in ret_sys_call
183 | pushl %eax
184 | movl $0x10,%eax
185 | mov %ax,%ds
186 | mov %ax,%es
187 | movl $0x17,%eax
188 | mov %ax,%fs
189 | incl jiffies
190 | movb $0x20,%al # EOI to interrupt controller #1
191 | outb %al,$0x20
192 | movl CS(%esp),%eax
193 | andl $3,%eax # %eax is CPL (0 or 3, 0=supervisor)
194 | pushl %eax
195 | call do_timer # 'do_timer(long CPL)' does everything from
196 | addl $4,%esp # task switching to accounting ...
197 | jmp ret_from_sys_call
198 |
199 | .align 2
200 | sys_execve:
201 | lea EIP(%esp),%eax
202 | pushl %eax
203 | call do_execve
204 | addl $4,%esp
205 | ret
206 |
207 | .align 2
208 | sys_fork:
209 | call find_empty_process
210 | testl %eax,%eax
211 | js 1f
212 | push %gs
213 | pushl %esi
214 | pushl %edi
215 | pushl %ebp
216 | pushl %eax
217 | call copy_process
218 | addl $20,%esp
219 | 1: ret
220 |
221 | hd_interrupt:
222 | pushl %eax
223 | pushl %ecx
224 | pushl %edx
225 | push %ds
226 | push %es
227 | push %fs
228 | movl $0x10,%eax
229 | mov %ax,%ds
230 | mov %ax,%es
231 | movl $0x17,%eax
232 | mov %ax,%fs
233 | movb $0x20,%al
234 | outb %al,$0xA0 # EOI to interrupt controller #1
235 | jmp 1f # give port chance to breathe
236 | 1: jmp 1f
237 | 1: xorl %edx,%edx
238 | xchgl do_hd,%edx
239 | testl %edx,%edx
240 | jne 1f
241 | movl $unexpected_hd_interrupt,%edx
242 | 1: outb %al,$0x20
243 | call *%edx # "interesting" way of handling intr.
244 | pop %fs
245 | pop %es
246 | pop %ds
247 | popl %edx
248 | popl %ecx
249 | popl %eax
250 | iret
251 |
252 | floppy_interrupt:
253 | pushl %eax
254 | pushl %ecx
255 | pushl %edx
256 | push %ds
257 | push %es
258 | push %fs
259 | movl $0x10,%eax
260 | mov %ax,%ds
261 | mov %ax,%es
262 | movl $0x17,%eax
263 | mov %ax,%fs
264 | movb $0x20,%al
265 | outb %al,$0x20 # EOI to interrupt controller #1
266 | xorl %eax,%eax
267 | xchgl do_floppy,%eax
268 | testl %eax,%eax
269 | jne 1f
270 | movl $unexpected_floppy_interrupt,%eax
271 | 1: call *%eax # "interesting" way of handling intr.
272 | pop %fs
273 | pop %es
274 | pop %ds
275 | popl %edx
276 | popl %ecx
277 | popl %eax
278 | iret
279 |
280 | parallel_interrupt:
281 | pushl %eax
282 | movb $0x20,%al
283 | outb %al,$0x20
284 | popl %eax
285 | iret
286 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/traps.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/traps.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /*
8 | * 'Traps.c' handles hardware traps and faults after we have saved some
9 | * state in 'asm.s'. Currently mostly a debugging-aid, will be extended
10 | * to mainly kill the offending process (probably by giving it a signal,
11 | * but possibly by killing it outright if necessary).
12 | */
13 | #include
14 |
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #define get_seg_byte(seg,addr) ({ \
23 | register char __res; \
24 | __asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
25 | :"=a" (__res):"0" (seg),"m" (*(addr))); \
26 | __res;})
27 |
28 | #define get_seg_long(seg,addr) ({ \
29 | register unsigned long __res; \
30 | __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
31 | :"=a" (__res):"0" (seg),"m" (*(addr))); \
32 | __res;})
33 |
34 | #define _fs() ({ \
35 | register unsigned short __res; \
36 | __asm__("mov %%fs,%%ax":"=a" (__res):); \
37 | __res;})
38 |
39 | int do_exit(long code);
40 |
41 | void page_exception(void);
42 |
43 | void divide_error(void);
44 | void debug(void);
45 | void nmi(void);
46 | void int3(void);
47 | void overflow(void);
48 | void bounds(void);
49 | void invalid_op(void);
50 | void device_not_available(void);
51 | void double_fault(void);
52 | void coprocessor_segment_overrun(void);
53 | void invalid_TSS(void);
54 | void segment_not_present(void);
55 | void stack_segment(void);
56 | void general_protection(void);
57 | void page_fault(void);
58 | void coprocessor_error(void);
59 | void reserved(void);
60 | void parallel_interrupt(void);
61 | void irq13(void);
62 |
63 | static void die(char * str,long esp_ptr,long nr)
64 | {
65 | long * esp = (long *) esp_ptr;
66 | int i;
67 |
68 | printk("%s: %04x\n\r",str,nr&0xffff);
69 | printk("EIP:\t%04x:%p\nEFLAGS:\t%p\nESP:\t%04x:%p\n",
70 | esp[1],esp[0],esp[2],esp[4],esp[3]);
71 | printk("fs: %04x\n",_fs());
72 | printk("base: %p, limit: %p\n",get_base(current->ldt[1]),get_limit(0x17));
73 | if (esp[4] == 0x17) {
74 | printk("Stack: ");
75 | for (i=0;i<4;i++)
76 | printk("%p ",get_seg_long(0x17,i+(long *)esp[3]));
77 | printk("\n");
78 | }
79 | str(i);
80 | printk("Pid: %d, process nr: %d\n\r",current->pid,0xffff & i);
81 | for(i=0;i<10;i++)
82 | printk("%02x ",0xff & get_seg_byte(esp[1],(i+(char *)esp[0])));
83 | printk("\n\r");
84 | do_exit(11); /* play segment exception */
85 | }
86 |
87 | void do_double_fault(long esp, long error_code)
88 | {
89 | die("double fault",esp,error_code);
90 | }
91 |
92 | void do_general_protection(long esp, long error_code)
93 | {
94 | die("general protection",esp,error_code);
95 | }
96 |
97 | void do_divide_error(long esp, long error_code)
98 | {
99 | die("divide error",esp,error_code);
100 | }
101 |
102 | void do_int3(long * esp, long error_code,
103 | long fs,long es,long ds,
104 | long ebp,long esi,long edi,
105 | long edx,long ecx,long ebx,long eax)
106 | {
107 | int tr;
108 |
109 | __asm__("str %%ax":"=a" (tr):"0" (0));
110 | printk("eax\t\tebx\t\tecx\t\tedx\n\r%8x\t%8x\t%8x\t%8x\n\r",
111 | eax,ebx,ecx,edx);
112 | printk("esi\t\tedi\t\tebp\t\tesp\n\r%8x\t%8x\t%8x\t%8x\n\r",
113 | esi,edi,ebp,(long) esp);
114 | printk("\n\rds\tes\tfs\ttr\n\r%4x\t%4x\t%4x\t%4x\n\r",
115 | ds,es,fs,tr);
116 | printk("EIP: %8x CS: %4x EFLAGS: %8x\n\r",esp[0],esp[1],esp[2]);
117 | }
118 |
119 | void do_nmi(long esp, long error_code)
120 | {
121 | die("nmi",esp,error_code);
122 | }
123 |
124 | void do_debug(long esp, long error_code)
125 | {
126 | die("debug",esp,error_code);
127 | }
128 |
129 | void do_overflow(long esp, long error_code)
130 | {
131 | die("overflow",esp,error_code);
132 | }
133 |
134 | void do_bounds(long esp, long error_code)
135 | {
136 | die("bounds",esp,error_code);
137 | }
138 |
139 | void do_invalid_op(long esp, long error_code)
140 | {
141 | die("invalid operand",esp,error_code);
142 | }
143 |
144 | void do_device_not_available(long esp, long error_code)
145 | {
146 | die("device not available",esp,error_code);
147 | }
148 |
149 | void do_coprocessor_segment_overrun(long esp, long error_code)
150 | {
151 | die("coprocessor segment overrun",esp,error_code);
152 | }
153 |
154 | void do_invalid_TSS(long esp,long error_code)
155 | {
156 | die("invalid TSS",esp,error_code);
157 | }
158 |
159 | void do_segment_not_present(long esp,long error_code)
160 | {
161 | die("segment not present",esp,error_code);
162 | }
163 |
164 | void do_stack_segment(long esp,long error_code)
165 | {
166 | die("stack segment",esp,error_code);
167 | }
168 |
169 | void do_coprocessor_error(long esp, long error_code)
170 | {
171 | if (last_task_used_math != current)
172 | return;
173 | die("coprocessor error",esp,error_code);
174 | }
175 |
176 | void do_reserved(long esp, long error_code)
177 | {
178 | die("reserved (15,17-47) error",esp,error_code);
179 | }
180 |
181 | void trap_init(void)
182 | {
183 | int i;
184 |
185 | set_trap_gate(0,÷_error);
186 | set_trap_gate(1,&debug);
187 | set_trap_gate(2,&nmi);
188 | set_system_gate(3,&int3); /* int3-5 can be called from all */
189 | set_system_gate(4,&overflow);
190 | set_system_gate(5,&bounds);
191 | set_trap_gate(6,&invalid_op);
192 | set_trap_gate(7,&device_not_available);
193 | set_trap_gate(8,&double_fault);
194 | set_trap_gate(9,&coprocessor_segment_overrun);
195 | set_trap_gate(10,&invalid_TSS);
196 | set_trap_gate(11,&segment_not_present);
197 | set_trap_gate(12,&stack_segment);
198 | set_trap_gate(13,&general_protection);
199 | set_trap_gate(14,&page_fault);
200 | set_trap_gate(15,&reserved);
201 | set_trap_gate(16,&coprocessor_error);
202 | for (i=17;i<48;i++)
203 | set_trap_gate(i,&reserved);
204 | set_trap_gate(45,&irq13);
205 | outb_p(inb_p(0x21)&0xfb,0x21);
206 | outb(inb_p(0xA1)&0xdf,0xA1);
207 | set_trap_gate(39,¶llel_interrupt);
208 | }
209 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/traps.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/traps.o
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/vsprintf.c:
--------------------------------------------------------------------------------
1 | /*
2 | * linux/kernel/vsprintf.c
3 | *
4 | * (C) 1991 Linus Torvalds
5 | */
6 |
7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 | /*
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 | */
11 |
12 | #include
13 | #include
14 |
15 | /* we use this so that we can do without the ctype library */
16 | #define is_digit(c) ((c) >= '0' && (c) <= '9')
17 |
18 | static int skip_atoi(const char **s)
19 | {
20 | int i=0;
21 |
22 | while (is_digit(**s))
23 | i = i*10 + *((*s)++) - '0';
24 | return i;
25 | }
26 |
27 | #define ZEROPAD 1 /* pad with zero */
28 | #define SIGN 2 /* unsigned/signed long */
29 | #define PLUS 4 /* show plus */
30 | #define SPACE 8 /* space if plus */
31 | #define LEFT 16 /* left justified */
32 | #define SPECIAL 32 /* 0x */
33 | #define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */
34 |
35 | #define do_div(n,base) ({ \
36 | int __res; \
37 | __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
38 | __res; })
39 |
40 | static char * number(char * str, int num, int base, int size, int precision
41 | ,int type)
42 | {
43 | char c,sign,tmp[36];
44 | const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
45 | int i;
46 |
47 | if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
48 | if (type&LEFT) type &= ~ZEROPAD;
49 | if (base<2 || base>36)
50 | return 0;
51 | c = (type & ZEROPAD) ? '0' : ' ' ;
52 | if (type&SIGN && num<0) {
53 | sign='-';
54 | num = -num;
55 | } else
56 | sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
57 | if (sign) size--;
58 | if (type&SPECIAL) {
59 | if (base==16) size -= 2;
60 | else if (base==8) size--;
61 | }
62 | i=0;
63 | if (num==0)
64 | tmp[i++]='0';
65 | else while (num!=0)
66 | tmp[i++]=digits[do_div(num,base)];
67 | if (i>precision) precision=i;
68 | size -= precision;
69 | if (!(type&(ZEROPAD+LEFT)))
70 | while(size-->0)
71 | *str++ = ' ';
72 | if (sign)
73 | *str++ = sign;
74 | if (type&SPECIAL) {
75 | if (base==8)
76 | *str++ = '0';
77 | else if (base==16) {
78 | *str++ = '0';
79 | *str++ = digits[33];
80 | }
81 | }
82 | if (!(type&LEFT))
83 | while(size-->0)
84 | *str++ = c;
85 | while(i0)
88 | *str++ = tmp[i];
89 | while(size-->0)
90 | *str++ = ' ';
91 | return str;
92 | }
93 |
94 | int vsprintf(char *buf, const char *fmt, va_list args)
95 | {
96 | int len;
97 | int i;
98 | char * str;
99 | char *s;
100 | int *ip;
101 |
102 | int flags; /* flags to number() */
103 |
104 | int field_width; /* width of output field */
105 | int precision; /* min. # of digits for integers; max
106 | number of chars for from string */
107 | int qualifier; /* 'h', 'l', or 'L' for integer fields */
108 |
109 | for (str=buf ; *fmt ; ++fmt) {
110 | if (*fmt != '%') {
111 | *str++ = *fmt;
112 | continue;
113 | }
114 |
115 | /* process flags */
116 | flags = 0;
117 | repeat:
118 | ++fmt; /* this also skips first '%' */
119 | switch (*fmt) {
120 | case '-': flags |= LEFT; goto repeat;
121 | case '+': flags |= PLUS; goto repeat;
122 | case ' ': flags |= SPACE; goto repeat;
123 | case '#': flags |= SPECIAL; goto repeat;
124 | case '0': flags |= ZEROPAD; goto repeat;
125 | }
126 |
127 | /* get field width */
128 | field_width = -1;
129 | if (is_digit(*fmt))
130 | field_width = skip_atoi(&fmt);
131 | else if (*fmt == '*') {
132 | /* it's the next argument */
133 | field_width = va_arg(args, int);
134 | if (field_width < 0) {
135 | field_width = -field_width;
136 | flags |= LEFT;
137 | }
138 | }
139 |
140 | /* get the precision */
141 | precision = -1;
142 | if (*fmt == '.') {
143 | ++fmt;
144 | if (is_digit(*fmt))
145 | precision = skip_atoi(&fmt);
146 | else if (*fmt == '*') {
147 | /* it's the next argument */
148 | precision = va_arg(args, int);
149 | }
150 | if (precision < 0)
151 | precision = 0;
152 | }
153 |
154 | /* get the conversion qualifier */
155 | qualifier = -1;
156 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
157 | qualifier = *fmt;
158 | ++fmt;
159 | }
160 |
161 | switch (*fmt) {
162 | case 'c':
163 | if (!(flags & LEFT))
164 | while (--field_width > 0)
165 | *str++ = ' ';
166 | *str++ = (unsigned char) va_arg(args, int);
167 | while (--field_width > 0)
168 | *str++ = ' ';
169 | break;
170 |
171 | case 's':
172 | s = va_arg(args, char *);
173 | len = strlen(s);
174 | if (precision < 0)
175 | precision = len;
176 | else if (len > precision)
177 | len = precision;
178 |
179 | if (!(flags & LEFT))
180 | while (len < field_width--)
181 | *str++ = ' ';
182 | for (i = 0; i < len; ++i)
183 | *str++ = *s++;
184 | while (len < field_width--)
185 | *str++ = ' ';
186 | break;
187 |
188 | case 'o':
189 | str = number(str, va_arg(args, unsigned long), 8,
190 | field_width, precision, flags);
191 | break;
192 |
193 | case 'p':
194 | if (field_width == -1) {
195 | field_width = 8;
196 | flags |= ZEROPAD;
197 | }
198 | str = number(str,
199 | (unsigned long) va_arg(args, void *), 16,
200 | field_width, precision, flags);
201 | break;
202 |
203 | case 'x':
204 | flags |= SMALL;
205 | case 'X':
206 | str = number(str, va_arg(args, unsigned long), 16,
207 | field_width, precision, flags);
208 | break;
209 |
210 | case 'd':
211 | case 'i':
212 | flags |= SIGN;
213 | case 'u':
214 | str = number(str, va_arg(args, unsigned long), 10,
215 | field_width, precision, flags);
216 | break;
217 |
218 | case 'n':
219 | ip = va_arg(args, int *);
220 | *ip = (str - buf);
221 | break;
222 |
223 | default:
224 | if (*fmt != '%')
225 | *str++ = '%';
226 | if (*fmt)
227 | *str++ = *fmt;
228 | else
229 | --fmt;
230 | break;
231 | }
232 | }
233 | *str = '\0';
234 | return str-buf;
235 | }
236 |
--------------------------------------------------------------------------------
/lab_2/release_HW/kernel/vsprintf.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/kernel/vsprintf.o
--------------------------------------------------------------------------------
/lab_2/release_HW/lab2_shell:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_2/release_HW/lab2_shell
--------------------------------------------------------------------------------
/lab_2/release_HW/lab2_shell.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | #define MAX_CMDLINE_LENGTH 256 /*max cmdline length in a line*/
8 | #define MAX_CMD_LENGTH 16 /*max single cmdline length*/
9 | #define MAX_CMD_NUM 16 /*max single cmdline length*/
10 |
11 | #ifndef NR_TASKS /*max task num*/
12 | #define NR_TASKS 64
13 | #endif
14 |
15 | #define SHELL "/bin/sh"
16 |
17 | static int *child_pid = NULL; /*save running children's pid*/
18 |
19 | /* popen,输入为命令和类型("r""w"),输出执行命令进程的I/O文件描述符 */
20 | int os_popen(const char* cmd, const char type){
21 | int i, pipe_fd[2], proc_fd;
22 | pid_t pid;
23 |
24 | if (type != 'r' && type != 'w') {
25 | printf("popen() flag error\n");
26 | return NULL;
27 | }
28 |
29 | if(child_pid == NULL) {
30 | if ((child_pid = calloc(NR_TASKS, sizeof(int))) == NULL)
31 | return NULL;
32 | }
33 |
34 | if (pipe(pipe_fd) < 0) {
35 | printf("popen() pipe create error\n");
36 | return NULL;
37 | }
38 |
39 | /* 1. 使用系统调用创建新进程 */
40 | pid = fork();
41 |
42 | /* 2. 子进程部分 */
43 | if (pid == 0)
44 | {
45 | if (type == 'r') {
46 | /* 2.1 关闭pipe无用的一端,将I/O输出发送到父进程 */
47 | close(pipe_fd[0]);
48 | if (pipe_fd[1] != STDOUT_FILENO) {
49 | dup2(pipe_fd[1], STDOUT_FILENO);
50 | close(pipe_fd[1]); /* 定向的参考位置,现在没有用了,故关闭 */
51 | }
52 | } else {
53 | /* 2.2 关闭pipe无用的一端,接收父进程提供的I/O输入 */
54 | close(pipe_fd[1]);
55 | if (pipe_fd[0] != STDIN_FILENO) {
56 | dup2(pipe_fd[0], STDIN_FILENO);
57 | close(pipe_fd[0]);
58 | }
59 | }
60 | /* 关闭所有未关闭的子进程文件描述符(无需修改) */
61 | for (i=0;i0)
63 | close(i);
64 | /* 2.3 通过exec系统调用运行命令 */
65 | execl(SHELL, "sh", "-c", cmd, (char *)NULL);
66 | /* 也可使用execlp execvp等 */
67 | _exit(127);
68 | }
69 | /* 3. 父进程部分 */
70 | else
71 | {
72 | if (type == 'r')
73 | {
74 | close(pipe_fd[1]);
75 | proc_fd = pipe_fd[0];
76 | }
77 | else
78 | {
79 | close(pipe_fd[0]);
80 | proc_fd = pipe_fd[1];
81 | }
82 | child_pid[proc_fd] = pid;
83 | return proc_fd;
84 | }
85 |
86 | }
87 |
88 | /* 关闭正在打开的管道,等待对应子进程运行结束(无需修改) */
89 | int os_pclose(const int fno) {
90 | int stat;
91 | pid_t pid;
92 | if (child_pid == NULL)
93 | return -1;
94 | if ((pid = child_pid[fno]) == 0)
95 | return -1;
96 | child_pid[fno] = 0;
97 | close(fno);
98 | while (waitpid(pid, &stat, 0)<0)
99 | if(errno != EINTR)
100 | return -1;
101 | return stat;
102 | }
103 |
104 | int os_system(const char* cmdstring) {
105 | pid_t pid;
106 | int stat;
107 | /*指令分解之后作为字符串存在每一个argv中,这类似一个指针数组*/
108 | char *argv[MAX_CMD_NUM];
109 | int cmd_no = 0;
110 | char *temp;
111 | int i;
112 | /*以空格,字符串结束符做分割标志符*/
113 | const char div_symbol[2] = {' ', '\0'};
114 | if (cmdstring == NULL)
115 | {
116 | printf("nothing to do\n");
117 | return 1;
118 | }
119 | for (i = 0; i < MAX_CMD_NUM; i++)
120 | {
121 | argv[i] = (char *)malloc(MAX_CMD_LENGTH * sizeof(char));
122 | }
123 | /* 4.1 创建一个新进程 */
124 | pid = fork();
125 |
126 | /* 4.2 子进程部分 */
127 | if (pid == 0)
128 | {
129 | temp = strtok(cmdstring, div_symbol);
130 | /*将以s作为分隔符的分隔后的指令作为参数存入argv*/
131 | while (temp)
132 | {
133 | strcpy(argv[cmd_no], temp);
134 | temp = strtok(NULL, div_symbol);
135 | cmd_no++;
136 | }
137 | /*argv开始时未初始化,为避免错误,参数接受结束后加NULL*/
138 | argv[cmd_no] = NULL;
139 | execvp(argv[0], argv);
140 | /*command not found,error code:127*/
141 | _exit(127);
142 | }
143 |
144 | /* 4.3 父进程部分: 等待子进程运行结束 */
145 | {
146 | /*在这里不必使用子进程返回状态符和第三个参数option*/
147 | waitpid(pid, NULL, 0);
148 | }
149 |
150 | return stat;
151 | }
152 |
153 | /* 对cmdline按照";"做划分,返回划分段数 */
154 | int parseCmd(char* cmdline, char cmds[MAX_CMD_NUM][MAX_CMD_LENGTH]) {
155 | int i,j;
156 | int offset = 0;
157 | int cmd_num = 0;
158 | char tmp[MAX_CMD_LENGTH];
159 | int len = NULL;
160 | char *end = strchr(cmdline, ';');
161 | char *start = cmdline;
162 | while (end != NULL) {
163 | memcpy(cmds[cmd_num], start, end - start);
164 | cmds[cmd_num++][end - start] = '\0';
165 |
166 | start = end + 1;
167 | end = strchr(start, ';');
168 | };
169 | len = strlen(cmdline);
170 | if (start < cmdline + len) {
171 | memcpy(cmds[cmd_num], start, (cmdline + len) - start);
172 | cmds[cmd_num++][(cmdline + len) - start] = '\0';
173 | }
174 | return cmd_num;
175 | }
176 |
177 | void zeroBuff(char* buff, int size) {
178 | int i;
179 | for(i=0;i", 11);
196 | gets(cmdline);
197 | cmd_num = parseCmd(cmdline, cmds);
198 | for(i=0;i /* 有它,编译器才能获知自定义的系统调用的编号 */
5 | #include
6 | #include
7 |
8 | _syscall1(int, print_val,int, a); /* print_val()在用户空间的接口函数, 下同 */
9 | _syscall3(int, str2num, char *, str, int, str_len, long *, ret);
10 |
11 | int main(int argc, char const *argv[])
12 | {
13 | long *lval = NULL;
14 | char cval[100];
15 | memset(cval, '\0', 100 * sizeof(char));
16 | printf("Give me a string:\n");
17 | scanf("%s", cval);
18 | str2num(cval, strlen(cval), lval);
19 | print_val(*lval);
20 | printf("\n");
21 | return 0;
22 | }
23 |
--------------------------------------------------------------------------------
/lab_2/summary.md:
--------------------------------------------------------------------------------
1 | # Summary for OS Lab 2
2 | by 鸢一折纸
3 |
4 | ## 添加 Linux 系统调用
5 |
6 | ### 分配系统调用号,修改系统调用表
7 |
8 | 首先,需要找到要修改的文件的位置,鉴于讲义比较含蓄,我建议去 Google(多看几遍或者问问)
9 |
10 | 需要保持命名格式统一。。。(比如`sys_balabala`)因为别的地方会有带着这些前缀的
11 |
12 | 需要添加的文件里面:
13 |
14 | - `system_call.s` 和 `sys_xxx` 都位于源码里面(就是 `~/oslab/Linux-0.11` 目录)=·=
15 | - `./kernel/system_calls.s`,参数加上2
16 | - `./include/linux/sys.h`,放最后面就行(原型和 table 表(就是最后面黑色的一大堆))
17 | - `unistd.h` 是既要在挂载的文件系统里面修改,又要在源码修改的
18 | - `./include/unistd.h`
19 | - `./hdc/usr/include/unistd.h`
20 |
21 | > 以及,关于为什么建议不要直接拷贝linux源码中的 `unistd.h` 文件,助教大大的官方解释是:内容其实是差不多的,但是Linux 0.11 不支持` // `这种形式的注释,但是Linux源码中的这个文件有这个注释,所以说需要换成`/* */`
22 |
23 | > 对题的注释(这段可能是我蠢了,不需要的直接跳过)
24 |
25 | 1. 题里面的`sys_xxx`是一个代称。。。没有真的什么叫`xxx`的
26 | 2. 讲义里面路径有点混乱诶 /摊手
27 |
28 | ### 实现系统调用函数
29 |
30 | #### 分析如下:
31 |
32 | > 转载并改编自XT大佬的提示@Xiao2002
33 |
34 |
35 |
36 | 在使用**print_val()**之前(比如在**test.c**文件中),我们应当先使用`_syscall1(int, print_val, int, a)`函数,**_syscallx**的x由调用函数的参数个数决定。该函数的目的是调用真正实现**print_val()**系统调用功能的函数(本例中为在**print_str2num.c**(xxx.c)文件中定义的`sys_print_val()`函数)。
37 |
38 | > 对于文件的编辑:两个`sys_`的函数在**xxx.c**文件中实现(系统调用);而实际上用户调用的不带`sys_`的函数在各自的**.c**文件(同放在kernel目录下)
39 |
40 |
41 |
42 | 然后根据`syscall1()`的宏定义,有关系统调用`print_val()`的各种参数被传入寄存器,其中参数**__NR_print_val**(上一问)被传入寄存器**%eax**。然后进行**0x80**中断调用,此时来到了**kernel/system_calls.s**文件中的**system_call**部分。执行到语句`call *sys_call_table(,%eax,4)`时,系统再次跳转到了**include/linux/sys.h**文件中的**sys_call_table[]**数组中,并将寄存器**%eax**的值传给该数组,寻找对应的真正实现系统调用功能的函数`sys_print_val()`。可知之前定义的参数**__NR_print_val**的值便是数组**sys_call_table[]**中真正实现系统调用功能的函数`sys_print_val()`的下标,然后操作系统便会到其他文件中去寻找该函数的实现代码(这里是**print_str2num.c**文件)。
43 |
44 | > 为什么会这样呢?实际上,回顾一下之前有关Kernel态和User态的课程可知,这次 I/O 请求需要通过内核态,于是**test**调用`sys_print_val`(stdlib中声明),而这个函数的实现是通过`_syscallx`系列,传参了`print_val`来完成的
45 |
46 |
47 |
48 | 在这一切的调用都结束后,我们回到**test.c**文件中。
49 |
50 | #### 编写系统调用实现
51 |
52 | ##### 总共实现1个文件
53 |
54 | - 也就是**xxx.c**
55 |
56 | 如果说前面那一堆没看懂的话,需要实现的表面来看是:
57 |
58 | - kernel目录下的一个系统调用函数文件(xxx.c),会用于makefile
59 | - 测试文件,里面调用用户函数,放到qemu
60 |
61 | ##### 需要有define和include
62 |
63 | ```c
64 | #define __LIBRARY__
65 | #include
66 | #include
67 | ```
68 |
69 | 其中,include的文件绝对路径就是在\<\>里面的前面加上`Linux-0.11/include`
70 |
71 | 至于为什么需要添加这几个头文件···
72 |
73 | - `#define __LIBRARY__`:在(include/unistd.h)中有叙述:
74 |
75 | - ```c
76 | #ifdef __LIBRARY
77 |
78 | ...
79 | define __NR_write 4 // 系统调用编号
80 | ...
81 | int write(int fildes, const char * buf, off_t count); // 系统调用原型
82 | ...
83 | ```
84 |
85 | - `asm/segment.h`:在makefile中添加的编译路径为这个文件(**如果我们修改了include的文件(如增加),需要在makefile文件中做对应的修改**.)
86 |
87 | - 不需要`#define put_fs_long`
88 |
89 | #### 修改Makefile
90 |
91 | xxx指上面那个**print_str2num**
92 |
93 | #### 编写测试程序
94 |
95 | 第一行的printf一定要加换行符,否则会出现那一行输出到了最后面的情况
96 |
97 | (薄弱的C语言基础暴露无遗=·=
98 |
99 | 使用**memset**函数将数组置为`'\0'`,而strlen函数读取到\0就会截止。。。
100 |
101 | ---
102 |
103 | ## 熟悉Linux下常见的系统调用函数
104 |
105 | 由于这一部分没有参考文档,建议参照习题课的Hint(PPT)
106 |
107 | 残留了一些问题,比如那个`execl`为什么这么填参数
108 |
109 | ### 使用上述系统调用实现三个进程创建相关的函数
110 |
111 | 这里需要写的并不多其实,其中**os_close**已经实现
112 |
113 | ##### `os_open()`
114 |
115 | fork函数,以及对于父子进程的**if**分支;一个 execl 函数调用;以及 pipe_fd 的填空
116 |
117 | `dup2`是一个重定向工作,将STDOUT定向到和管道出口一样的位置(或者IN和入口),参考:ch5 part1 37页前后
118 |
119 | ##### `os_system`
120 |
121 | emm依然不是很理解。。。
122 |
123 | ### Linux-0.11下的编译运行
124 |
125 | 一些可能出现的问题:
126 |
127 | - 出现`Kernel panic`:更换 hdc-0.11 镜像文件
128 | - 出现`parse error before 'print_val'`:加上 `__LIBRARY__`
129 | - 出现`lab2_shell: ENOENT`:是 error no entry 的缩写,就是没这个文件/目录;检查 gcc 的报错
130 | - 提交的时候没有权限:`sudo chmod 777 `即可
--------------------------------------------------------------------------------
/lab_3/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/.DS_Store
--------------------------------------------------------------------------------
/lab_3/PB18000227_艾语晨_lab3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/PB18000227_艾语晨_lab3.pdf
--------------------------------------------------------------------------------
/lab_3/Summary for lab 3.md:
--------------------------------------------------------------------------------
1 | # Summary for lab 3
2 |
3 | by 鸢一折纸
4 |
5 | ### 可能出现的问题
6 |
7 | > makefile 意外报错
8 |
9 | - `malformed input file`
10 | - 
11 | - 报错信息就说mm.o文件格式有问题 由于linux-0.11系统rm命令有问题 可以挂载之后 把生成的文件清楚一下 然后再编译一次试一下
12 | - `ENOENT`:没有那个文件或目录
13 | - 
14 | - 应该是makefile出现了问题(这个是makefile就没有生成mmdriver)
15 |
16 | > 测试文件报错
17 |
18 | - 90%的可能是在`place()`函数出现的问题
19 | - 检查PUT的指针指向位置
20 | - 有可能是没有`HDRP(bp)`,HDRP很容易被忽视,尤其是一大堆括号的时候
21 |
22 | > 其他
23 |
24 | 如果卸载文件系统出现busy:关闭所有窗口或者重启
25 |
26 | ### 关于堆空间的分配
27 |
28 | 堆分配的空间利用情况:总共5MB的堆空间,分成一个一个的块(占用、空闲)并用链表连接。块内部由头部、有效载荷、填充组成
29 |
30 | 这个总空间是5MB,从`mem_start_brk`到`mem_max_addr`,用`sbrk`来扩展
31 |
32 | 需要自己实现的链表分配是在这个堆内部的一部分空间 (即原文档第7页的`Mymalloc`),放了一个链表,调用`mem_brk` (封装为`extend_heap`) 来进行扩容
33 |
34 | > 关于指针
35 |
36 | 
37 |
38 | 箭头所指的位置是bp指针指向的,在有效载荷的首位
--------------------------------------------------------------------------------
/lab_3/lab3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/lab3.pdf
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # Students' Makefile for the Malloc Lab
3 | # it will generate two executable files
4 | # mmdriver tests the implicit linked-list
5 | # ep_mmdriver tests the explicit linked-list
6 | #
7 |
8 | CC = gcc -g
9 | CFLAGS = -Wall
10 |
11 | OBJS1 = memlib.o mmdriver.o mm.o
12 | OBJS2 = memlib.o ep_mmdriver.o ep_mm.o
13 | OBJS_ALL = memlib.o mmdriver.o ep_mmdriver.o mm.o ep_mm.o
14 |
15 | all : mmdriver ep_mmdriver
16 |
17 | mmdriver : $(OBJS1)
18 | cc -o mmdriver $(OBJS1)
19 | ep_mmdriver : $(OBJS2)
20 | cc -o ep_mmdriver $(OBJS2)
21 |
22 | memlib.o : memlib.h config.h
23 | mm_o : mm.h memlib.h
24 | ep_mm.o : ep_mm.h memlib.h
25 | mmdriver.o : mm.h memlib.h
26 | ep_mmdriver.o : ep_mm.h memlib.h
27 |
28 | .PHONY : clean
29 | clean:
30 | -rm mmdriver ep_mmdriver $(OBJS_ALL)
31 |
32 |
33 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/README:
--------------------------------------------------------------------------------
1 |
2 | ep_mm.c中是显示空闲链表的实现
3 | mm.c 中是隐式空闲链表的实现
4 |
5 | 未实现:
6 | 分离的空闲链表(分离适配 or 伙伴系统)
7 |
8 |
9 | ***********
10 | Main Files:
11 | ***********
12 |
13 | mm.{c,h}
14 | Your solution malloc package. mm.c is the file that you
15 | will be handing in, and is the only file you should modify.
16 |
17 | mdriver.c
18 | The malloc driver that tests your mm.c file
19 |
20 | ./trace/1,2.rep
21 | Two tiny tracefiles to help you get started.
22 |
23 | Makefile
24 | Builds the driver
25 |
26 | **********************************
27 | Other support files for the driver
28 | **********************************
29 |
30 | memlib.{c,h} Models the heap and sbrk function
31 |
32 | *******************************
33 | Building and running the driver
34 | *******************************
35 | To build the driver, type "make" to the shell.
36 |
37 | To run the driver on a tiny test trace:
38 |
39 | unix> ./mdriver
40 |
41 |
42 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/a.out:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/lab3_malloc/a.out
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/config.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONFIG_H_
2 | #define __CONFIG_H_
3 |
4 | /*
5 | * config.h - malloc lab configuration file
6 | *
7 | * Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved.
8 | * May not be used, modified, or copied without permission.
9 | */
10 |
11 | /*
12 | * This is the default path where the driver will look for the
13 | * default tracefiles. You can override it at runtime with the -t flag.
14 | */
15 | /* #define TRACEDIR "/afs/cs/project/ics2/im/labs/malloclab/traces/"*/
16 | #define TRACEDIR "./traces/"
17 |
18 | /*
19 | * This is the list of default tracefiles in TRACEDIR that the driver
20 | * will use for testing. Modify this if you want to add or delete
21 | * traces from the driver's test suite. For example, if you don't want
22 | * your students to implement realloc, you can delete the last two
23 | * traces.
24 | */
25 |
26 | #define DEFAULT_TRACEFILES \
27 | "1.rep"
28 | /* "random-bal.rep", \
29 | "random2-bal.rep"
30 |
31 | "realloc-bal.rep",
32 | "realloc2-bal.rep"
33 | #define DEFAULT_TRACEFILES "cccp-bal.rep"*/
34 |
35 | /*
36 | * This constant gives the estimated performance of the libc malloc
37 | * package using our traces on some reference system, typically the
38 | * same kind of system the students use. Its purpose is to cap the
39 | * contribution of throughput to the performance index. Once the
40 | * students surpass the AVG_LIBC_THRUPUT, they get no further benefit
41 | * to their score. This deters students from building extremely fast,
42 | * but extremely stupid malloc packages.
43 | */
44 | #define AVG_LIBC_THRUPUT 600E3 /* 600 Kops/sec */
45 |
46 | /*
47 | * This constant determines the contributions of space utilization
48 | * (UTIL_WEIGHT) and throughput (1 - UTIL_WEIGHT) to the performance
49 | * index.
50 | */
51 | #define UTIL_WEIGHT .60
52 |
53 | /*
54 | * Alignment requirement in bytes (either 4 or 8)
55 | */
56 | #define ALIGNMENT 8
57 |
58 | /*
59 | * Maximum heap size in bytes
60 | */
61 | #define MAX_HEAP (5* (1 << 20)) /* 5 MB */
62 |
63 | /*****************************************************************************
64 | * Set exactly one of these USE_xxx constants to "1" to select a timing method
65 | *****************************************************************************/
66 | #define USE_FCYC 0 /* cycle counter w/K-best scheme (x86 & Alpha only) */
67 | #define USE_ITIMER 0 /* interval timer (any Unix box) */
68 | #define USE_GETTOD 1 /* gettimeofday (any Unix box) */
69 |
70 | #endif /* __CONFIG_H */
71 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/ep_mm.h:
--------------------------------------------------------------------------------
1 | #ifndef EP_MM_H
2 | #define EP_MM_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | #include "mm.h"
11 | #include "memlib.h"
12 |
13 | extern int mm_init(void);
14 | extern void *mm_malloc(size_t size);
15 | extern void mm_free(void *bp);
16 | extern void *mm_realloc(void *ptr, size_t size);
17 | /*
18 | * static void *extend_heap(size_t words);
19 | * static void *coalesce(void *bp);
20 | * static void *find_fit(size_t asize);
21 | * static void place(void *bp, size_t asize);
22 | * static void add_to_free_list(void *bp);
23 | * static void delete_from_free_list(void *bp);
24 | */
25 | extern void check_all_mem();
26 | extern void check_free_mem();
27 |
28 | #endif
29 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/ep_mmdriver.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include "ep_mm.h"
10 | #include "memlib.h"
11 |
12 | /* Misc */
13 | #define MAXLINE 1024 /* max string size */
14 | #define HDRLINES 4 /* number of header lines in a trace file */
15 | #define LINENUM(i) (i + 5) /* cnvt trace request nums to linenums (origin 1) */
16 | /*
17 | * Alignment requirement in bytes (either 4 or 8)
18 | */
19 | #define ALIGNMENT 8
20 |
21 | /* Returns true if p is ALIGNMENT-byte aligned */
22 | #define IS_ALIGNED(p) ((((unsigned int)(p)) % ALIGNMENT) == 0)
23 |
24 | /********************
25 | * Global variables
26 | *******************/
27 | int verbose = 0; /* global flag for verbose output */
28 | static int errors = 0; /* number of errs found when running student malloc */
29 | char msg[MAXLINE]; /* for whenever we need to compose an error message */
30 |
31 | /******************************
32 | * The key compound data types
33 | *****************************/
34 |
35 | /* Records the extent of each block's payload */
36 | typedef struct range_t
37 | {
38 | char *lo; /* low payload address */
39 | char *hi; /* high payload address */
40 | struct range_t *next; /* next list element */
41 | } range_t;
42 |
43 | /* Characterizes a single trace operation (allocator request) */
44 | typedef struct
45 | {
46 | enum
47 | {
48 | ALLOC,
49 | FREE,
50 | REALLOC
51 | } type; /* type of request */
52 | int index; /* index for free() to use later */
53 | int size; /* byte size of alloc/realloc request */
54 | } traceop_t;
55 |
56 | /* Holds the information for one trace file*/
57 | typedef struct
58 | {
59 | int sugg_heapsize; /* suggested heap size (unused) */
60 | int num_ids; /* number of alloc/realloc ids */
61 | int num_ops; /* number of distinct requests */
62 | int weight; /* weight for this trace (unused) */
63 | traceop_t *ops; /* array of requests */
64 | char **blocks; /* array of ptrs returned by malloc/realloc... */
65 | size_t *block_sizes; /* ... and a corresponding array of payload sizes */
66 | } trace_t;
67 |
68 | typedef struct
69 | {
70 | /* defined for both libc malloc and student malloc package (mm.c) */
71 | double ops; /* number of ops (malloc/free/realloc) in the trace */
72 | int valid; /* was the trace processed correctly by the allocator? */
73 | double secs; /* number of secs needed to run the trace */
74 |
75 | /* defined only for the student malloc package */
76 | double util; /* space utilization for this trace (always 0 for libc) */
77 |
78 | /* Note: secs and util are only defined if valid is true */
79 | } stats_t;
80 |
81 | static int add_range(range_t **ranges, char *lo, int size,
82 | int tracenum, int opnum);
83 |
84 | static void remove_range(range_t **ranges, char *lo);
85 |
86 | static void clear_ranges(range_t **ranges);
87 |
88 | static trace_t *read_trace(char *filename);
89 |
90 | void free_trace(trace_t *trace);
91 |
92 | static int eval_mm_valid(trace_t *trace, int tracenum, range_t **ranges);
93 |
94 | void app_error(char *msg);
95 |
96 | void unix_error(char *msg);
97 |
98 | void malloc_error(int tracenum, int opnum, char *msg);
99 |
100 | int run_trace(char *filename);
101 |
102 |
103 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/memlib.c:
--------------------------------------------------------------------------------
1 | /*
2 | * memlib.c - a module that simulates the memory system. Needed because it
3 | * allows us to interleave calls from the student's malloc package
4 | * with the system's malloc package in libc.
5 | */
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | #include "memlib.h"
14 | #include "config.h"
15 |
16 | /* private variables */
17 | static char *mem_start_brk; /* points to first byte of heap */
18 | static char *mem_brk; /* points to last byte of heap */
19 | static char *mem_max_addr; /* largest legal heap address */
20 |
21 | /*
22 | * mem_init - initialize the memory system model
23 | */
24 | void mem_init(void)
25 | {
26 | /* allocate the storage we will use to model the available VM */
27 | if ((mem_start_brk = (char *)malloc(MAX_HEAP)) == NULL) {
28 | fprintf(stderr, "mem_init_vm: malloc error\n");
29 | exit(1);
30 | }
31 |
32 | mem_max_addr = mem_start_brk + MAX_HEAP; /* max legal heap address */
33 | mem_brk = mem_start_brk; /* heap is empty initially */
34 | }
35 |
36 | /*
37 | * mem_deinit - free the storage used by the memory system model
38 | */
39 | void mem_deinit(void)
40 | {
41 | free(mem_start_brk);
42 | }
43 |
44 | /*
45 | * mem_reset_brk - reset the simulated brk pointer to make an empty heap
46 | */
47 | void mem_reset_brk()
48 | {
49 | mem_brk = mem_start_brk;
50 | }
51 |
52 | /*
53 | * mem_sbrk - simple model of the sbrk function. Extends the heap
54 | * by incr bytes and returns the start address of the new area. In
55 | * this model, the heap cannot be shrunk.
56 | */
57 | void *mem_sbrk(int incr)
58 | {
59 | char *old_brk = mem_brk;
60 |
61 | if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {
62 | errno = ENOMEM;
63 | fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n");
64 | return (void *)-1;
65 | }
66 | mem_brk += incr;
67 | return (void *)old_brk;
68 | }
69 |
70 | /*
71 | * mem_heap_lo - return address of the first heap byte
72 | */
73 | void *mem_heap_lo()
74 | {
75 | return (void *)mem_start_brk;
76 | }
77 |
78 | /*
79 | * mem_heap_hi - return address of last heap byte
80 | */
81 | void *mem_heap_hi()
82 | {
83 | return (void *)(mem_brk - 1);
84 | }
85 |
86 | /*
87 | * mem_heapsize() - returns the heap size in bytes
88 | */
89 | size_t mem_heapsize()
90 | {
91 | return (size_t)(mem_brk - mem_start_brk);
92 | }
93 |
94 | /*
95 | * mem_pagesize() - returns the page size of the system
96 | */
97 | size_t mem_pagesize()
98 | {
99 | /*return (size_t)getpagesize();*/
100 | return 4096;
101 | }
102 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/memlib.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void mem_init(void);
4 | void mem_deinit(void);
5 | void *mem_sbrk(int incr);
6 | void mem_reset_brk(void);
7 | void *mem_heap_lo(void);
8 | void *mem_heap_hi(void);
9 | size_t mem_heapsize(void);
10 | size_t mem_pagesize(void);
11 |
12 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/mm.c:
--------------------------------------------------------------------------------
1 | /*
2 | * mm-naive.c - The fastest, least memory-efficient malloc package.
3 | *
4 | * In this naive approach, a block is allocated by simply incrementing
5 | * the brk pointer. A block is pure payload. There are no headers or
6 | * footers. Blocks are never coalesced or reused. Realloc is
7 | * implemented directly using mm_malloc and mm_free.
8 | *
9 | * NOTE TO STUDENTS: Replace this header comment with your own header
10 | * comment that gives a high level description of your solution.
11 | */
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include "mm.h"
19 | #include "memlib.h"
20 |
21 | #define WSIZE 4
22 | #define DSIZE 8
23 | #define CHUNKSIZE (1 << 12)
24 | #define MAX(x, y) ((x) > (y) ? (x) : (y))
25 |
26 | #define PACK(size, alloc) ((size) | (alloc))
27 |
28 | #define GET(p) (*(unsigned int *)(p))
29 | #define PUT(p, val) (*(unsigned int *)(p) = (val))
30 |
31 | #define GET_SIZE(p) (GET(p) & ~0x7)
32 | #define GET_ALLOC(p) (GET(p) & 0x1)
33 |
34 | #define HDRP(bp) ((char *)(bp)-WSIZE)
35 | #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
36 |
37 | #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp)-WSIZE)))
38 | #define PREV_BLKP(bp) ((char *)(bp)-GET_SIZE(((char *)(bp)-DSIZE)))
39 |
40 | #define MIN_BLK_SIZE (2 * DSIZE)
41 |
42 | /* single word (4) or double word (8) alignment */
43 | #define ALIGNMENT DSIZE
44 |
45 | /* rounds up to the nearest multiple of ALIGNMENT */
46 | #define ALIGN(size) (((size) + (ALIGNMENT - 1)) & ~0x7)
47 |
48 | #define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
49 |
50 | static char *heap_listp;
51 |
52 | static void *extend_heap(size_t words);
53 | static void *coalesce(void *bp);
54 | static void *find_fit(size_t asize);
55 | static void place(void *bp, size_t asize);
56 |
57 | /*
58 | * mm_init - initialize the malloc package.
59 | */
60 | int mm_init(void)
61 | {
62 | if ((heap_listp = mem_sbrk(4 * WSIZE)) == (void *)-1)
63 | return -1;
64 | PUT(heap_listp, 0);
65 | PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1));
66 | PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1));
67 | PUT(heap_listp + (3 * WSIZE), PACK(0, 1));
68 | heap_listp += (2 * WSIZE);
69 |
70 | if (extend_heap(CHUNKSIZE / WSIZE) == NULL)
71 | return -1;
72 |
73 | return 0;
74 | }
75 |
76 | static void *extend_heap(size_t words)
77 | {
78 | char *bp;
79 | size_t size;
80 | size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;
81 | if ((long)(bp = mem_sbrk(size)) == -1)
82 | return NULL;
83 | PUT(HDRP(bp), PACK(size, 0));
84 | PUT(FTRP(bp), PACK(size, 0));
85 | PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));
86 | return coalesce(bp);
87 | }
88 |
89 | /*
90 | * mm_malloc - Allocate a block by incrementing the brk pointer.
91 | * Always allocate a block whose size is a multiple of the alignment.
92 | */
93 | void *mm_malloc(size_t size)
94 | {
95 |
96 | size_t newsize;
97 | size_t extend_size;
98 | char *bp;
99 |
100 | if (size == 0)
101 | return NULL;
102 | newsize = ALIGN(size) + DSIZE;
103 | if ((bp = find_fit(newsize)) != NULL)
104 | {
105 | place(bp, newsize);
106 | return bp;
107 | }
108 |
109 | /* no fit found. */
110 | extend_size = MAX(newsize, CHUNKSIZE);
111 | if ((bp = extend_heap(extend_size / WSIZE)) == NULL)
112 | {
113 | return NULL;
114 | }
115 | place(bp, newsize);
116 | return bp;
117 | }
118 |
119 | static void *coalesce(void *bp)
120 | {
121 | size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
122 | size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
123 | size_t size = GET_SIZE(HDRP(bp));
124 | if (prev_alloc && next_alloc)
125 | {
126 | return bp;
127 | }
128 | else if (prev_alloc && !next_alloc)
129 | {
130 | size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
131 | PUT(HDRP(bp), PACK(size, 0));
132 | PUT(FTRP(bp), PACK(size, 0));
133 | }
134 | else if (!prev_alloc && next_alloc)
135 | {
136 | size += GET_SIZE(FTRP(PREV_BLKP(bp)));
137 | PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
138 | PUT(FTRP(bp), PACK(size, 0));
139 | bp = PREV_BLKP(bp);
140 | }
141 | else
142 | {
143 | size += GET_SIZE(HDRP(NEXT_BLKP(bp))) + GET_SIZE(FTRP((PREV_BLKP(bp))));
144 | PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
145 | PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
146 | bp = PREV_BLKP(bp);
147 | }
148 | return bp;
149 | }
150 |
151 | static void *find_fit(size_t asize)
152 | {
153 |
154 | char *start = heap_listp; /* the starting addr of a block */
155 | while (GET(HDRP(start)) != 1) /* this block is not hdr */
156 | {
157 | if (!(GET_ALLOC(HDRP(start))) && GET_SIZE(HDRP(start)) >= asize)
158 | { /* it's free and large enough */
159 | return (void *)start;
160 | }
161 | else
162 | {
163 | start = NEXT_BLKP(start);
164 | }
165 | }
166 | return NULL; /* leave the demanding-space-task to caller */
167 | }
168 | static void place(void *bp, size_t asize)
169 | {
170 | const size_t total_size = GET_SIZE(HDRP(bp));
171 | size_t rest = total_size - asize;
172 |
173 | if (rest >= MIN_BLK_SIZE)
174 | /* need split */
175 | {
176 | /* size_t true_allo_size; */
177 | /* Allocate an even number of words to maintain alignment */
178 | /* true_allo_size = (asize % 2) ? (asize + 1) * WSIZE : asize * WSIZE; */
179 | PUT(HDRP(bp), PACK(asize, 1)); /* head of new block */
180 | PUT(FTRP(bp), PACK(asize, 1)); /* foot of new block */
181 | PUT(HDRP(NEXT_BLKP(bp)), PACK(rest, 0)); /* head of rest block */
182 | PUT(FTRP(NEXT_BLKP(bp)), PACK(rest, 0)); /* foot of rest block */
183 | }
184 | else
185 | {
186 | PUT(HDRP(bp), PACK(total_size, 1)); /* head of new block (fillings included) */
187 | PUT(FTRP(bp), PACK(total_size, 1)); /* foot of new block */
188 | }
189 | }
190 |
191 | /*
192 | * mm_free - Freeing a block does nothing.
193 | */
194 | void mm_free(void *ptr)
195 | {
196 | size_t size = GET_SIZE(HDRP(ptr));
197 |
198 | PUT(HDRP(ptr), PACK(size, 0));
199 | PUT(FTRP(ptr), PACK(size, 0));
200 | coalesce(ptr);
201 | }
202 |
203 | /*
204 | * mm_realloc - Implemented simply in terms of mm_malloc and mm_free
205 | */
206 | void *mm_realloc(void *ptr, size_t size)
207 | {
208 | void *oldptr = ptr;
209 | void *newptr;
210 | size_t copySize;
211 |
212 | newptr = mm_malloc(size);
213 | if (newptr == NULL)
214 | return NULL;
215 | copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE);
216 | if (size < copySize)
217 | copySize = size;
218 | memcpy(newptr, oldptr, copySize);
219 | mm_free(oldptr);
220 | return newptr;
221 | }
222 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/mm.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | extern int mm_init (void);
4 | extern void *mm_malloc (size_t size);
5 | extern void mm_free (void *ptr);
6 | extern void *mm_realloc(void *ptr, size_t size);
7 |
8 |
--------------------------------------------------------------------------------
/lab_3/lab3_malloc/mmdriver.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include "mm.h"
10 | #include "memlib.h"
11 |
12 | /* Misc */
13 | #define MAXLINE 1024 /* max string size */
14 | #define HDRLINES 4 /* number of header lines in a trace file */
15 | #define LINENUM(i) (i + 5) /* cnvt trace request nums to linenums (origin 1) */
16 | /*
17 | * Alignment requirement in bytes (either 4 or 8)
18 | */
19 | #define ALIGNMENT 8
20 |
21 | /* Returns true if p is ALIGNMENT-byte aligned */
22 | #define IS_ALIGNED(p) ((((unsigned int)(p)) % ALIGNMENT) == 0)
23 |
24 | /********************
25 | * Global variables
26 | *******************/
27 | int verbose = 0; /* global flag for verbose output */
28 | static int errors = 0; /* number of errs found when running student malloc */
29 | char msg[MAXLINE]; /* for whenever we need to compose an error message */
30 |
31 | /******************************
32 | * The key compound data types
33 | *****************************/
34 |
35 | /* Records the extent of each block's payload */
36 | typedef struct range_t
37 | {
38 | char *lo; /* low payload address */
39 | char *hi; /* high payload address */
40 | struct range_t *next; /* next list element */
41 | } range_t;
42 |
43 | /* Characterizes a single trace operation (allocator request) */
44 | typedef struct
45 | {
46 | enum
47 | {
48 | ALLOC,
49 | FREE,
50 | REALLOC
51 | } type; /* type of request */
52 | int index; /* index for free() to use later */
53 | int size; /* byte size of alloc/realloc request */
54 | } traceop_t;
55 |
56 | /* Holds the information for one trace file*/
57 | typedef struct
58 | {
59 | int sugg_heapsize; /* suggested heap size (unused) */
60 | int num_ids; /* number of alloc/realloc ids */
61 | int num_ops; /* number of distinct requests */
62 | int weight; /* weight for this trace (unused) */
63 | traceop_t *ops; /* array of requests */
64 | char **blocks; /* array of ptrs returned by malloc/realloc... */
65 | size_t *block_sizes; /* ... and a corresponding array of payload sizes */
66 | } trace_t;
67 |
68 | typedef struct
69 | {
70 | /* defined for both libc malloc and student malloc package (mm.c) */
71 | double ops; /* number of ops (malloc/free/realloc) in the trace */
72 | int valid; /* was the trace processed correctly by the allocator? */
73 | double secs; /* number of secs needed to run the trace */
74 |
75 | /* defined only for the student malloc package */
76 | double util; /* space utilization for this trace (always 0 for libc) */
77 |
78 | /* Note: secs and util are only defined if valid is true */
79 | } stats_t;
80 |
81 | static int add_range(range_t **ranges, char *lo, int size,
82 | int tracenum, int opnum);
83 |
84 | static void remove_range(range_t **ranges, char *lo);
85 |
86 | static void clear_ranges(range_t **ranges);
87 |
88 | static trace_t *read_trace(char *filename);
89 |
90 | void free_trace(trace_t *trace);
91 |
92 | static int eval_mm_valid(trace_t *trace, int tracenum, range_t **ranges);
93 |
94 | void app_error(char *msg);
95 |
96 | void unix_error(char *msg);
97 |
98 | void malloc_error(int tracenum, int opnum, char *msg);
99 |
100 | int run_trace(char *filename);
101 |
102 |
103 |
--------------------------------------------------------------------------------
/lab_3/pics/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/.DS_Store
--------------------------------------------------------------------------------
/lab_3/pics/1C64ED04F0583A572319C8BF19BEF507.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/1C64ED04F0583A572319C8BF19BEF507.png
--------------------------------------------------------------------------------
/lab_3/pics/4C902BB2D48E2EA2CF0B7491A9536E6F.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/4C902BB2D48E2EA2CF0B7491A9536E6F.png
--------------------------------------------------------------------------------
/lab_3/pics/68B39952D24FD2D6BDE44CB22719F731.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/68B39952D24FD2D6BDE44CB22719F731.jpg
--------------------------------------------------------------------------------
/lab_3/pics/7E56877638AF7D85EE3AC7CD4DF41BE7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/7E56877638AF7D85EE3AC7CD4DF41BE7.png
--------------------------------------------------------------------------------
/lab_3/pics/FAFB6D6FBAD2A01122A1F38442F5F2E4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_3/pics/FAFB6D6FBAD2A01122A1F38442F5F2E4.png
--------------------------------------------------------------------------------
/lab_4/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_4/.DS_Store
--------------------------------------------------------------------------------
/lab_4/OS lab 4 report.md:
--------------------------------------------------------------------------------
1 | # OS lab 4 report
2 |
3 | PB18000227艾语晨
4 |
5 | ## 第一部分
6 |
7 | ### 实现只读的FAT
8 |
9 | #### 第一题(跑测试文件)
10 |
11 | 思路以及一些比较大的坑 (log) 都在注释里面,其中`find_root()`函数有一处修改:第434~437行
12 |
13 | ```c
14 | if (is_eq && Root->DIR_Attr == ATTR_DIRECTORY)
15 | {
16 | return find_subdir(fat16_ins, Root, paths, pathDepth, 2);
17 | }
18 | ```
19 |
20 | 其中436行最后一个参数由1改为2,因为在root函数中,currDepth的对应值为1
21 |
22 | 测试结果:
23 |
24 | 
25 |
26 | 
27 |
28 | 回答问题:
29 |
30 | 1. FAT16
31 |
32 | -
33 |
34 | ```c
35 | typedef struct
36 | {
37 | FILE *fd;
38 | DWORD FirstRootDirSecNum;
39 | DWORD FirstDataSector;
40 | BPB_BS Bpb;
41 | } FAT16;
42 | ```
43 |
44 | - fd是用于挂载磁盘镜像文件(实验中为已实现的FAT)
45 | - FirstRootDirSecNum是根目录的第一个扇区绝对编号
46 | - FirstDataSector是数据区(2号蔟开始)第一个扇区的绝对编号
47 | - BPB存储着整个文件系统的配置信息
48 |
49 | 第二个不会……
--------------------------------------------------------------------------------
/lab_4/OS lab 4 report.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_4/OS lab 4 report.pdf
--------------------------------------------------------------------------------
/lab_4/OS lab 4 summary.md:
--------------------------------------------------------------------------------
1 | # OS lab 4 summary
2 |
3 | by Tobiichi Origami
4 |
5 | ## 任务一
6 |
7 | ### 熟悉FUSE的使用
8 |
9 | #### 关于界面看起来死掉了
10 |
11 | 在执行 `./fusexmp -d fuse_test_dir` 时,会出现这个界面
12 |
13 | 
14 |
15 | 这个不是啥错的……继续往后做吧
16 |
17 | ### 熟悉FAT文件系统
18 |
19 | #### `path_split()`
20 |
21 | > 截断过长的文件名输入(8+3)
22 |
23 | `paths` 是二维数组,即 (8+3)*Byte 的二进制字符类型矩阵
24 |
25 | 总路径长度用string函数来获得
26 |
27 | 关于分开写路径到paths:
28 |
29 | - 首先:当前目录、上一级目录这两个分开考虑
30 | - 需要转换大小写
31 | - 需要给文件名和扩展名一个定位指针(用于确定文件名录入位置,即哪一个‘/’后面是文件名而不是目录名,和确定截断)
--------------------------------------------------------------------------------
/lab_4/lab4-file-system.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_4/lab4-file-system.pdf
--------------------------------------------------------------------------------
/lab_4/pics/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_4/pics/.DS_Store
--------------------------------------------------------------------------------
/lab_4/pics/4_1_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OrigamiAyc/Operating-System/db530b25d9d59026186e0c5714a69d44c13304eb/lab_4/pics/4_1_1.png
--------------------------------------------------------------------------------