You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/Computer Organisation/Processor/Register.md
+2-3Lines changed: 2 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ tags:
7
7
- OS
8
8
- computer_organisation
9
9
Creation Date: 2023-11-20T10:58:00
10
-
Last Date: 2024-02-20T13:21:32+08:00
10
+
Last Date: 2024-04-01T19:23:27+08:00
11
11
References:
12
12
---
13
13
## Abstract
@@ -30,8 +30,7 @@ References:
30
30
- CSRs are typically used to control various aspects of the processor's operation, such as interrupt handling, memory management, and power management etc
31
31
32
32
### Stack Pointer
33
-
- A [[Register]]
34
-
- Holds the address of the top of the [[Address Space#Stack Segment]] in the current **execution context**
33
+
- A [[Register]] that holds the [[Memory Address]] of the top of the [[Address Space#Stack Segment]] in the current **execution context**. Here is the [[stack_segment.png|Diagram]]
35
34
36
35
>[!info]`offset($sp)`
37
36
> Used to **access a memory location****relative** to the **current top** of the stack
Copy file name to clipboardExpand all lines: content/OS/Process/Address Space.md
+27-18Lines changed: 27 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ tags:
9
9
- c
10
10
- rust
11
11
Creation Date: 2023-10-19T17:15:00
12
-
Last Date: 2024-03-17T13:59:16+08:00
12
+
Last Date: 2024-04-01T19:53:43+08:00
13
13
References:
14
14
description: Stack (automatic memory management for function variables), Heap (dynamic memory management), Data (stores pre-defined variables shipped with the program) and Text (stores unchangeable program codes).
15
15
---
@@ -23,7 +23,8 @@ description: Stack (automatic memory management for function variables), Heap (d
23
23
- Data in [[#Text Segment]] (the **orange** block shown above) and [[#Data Segment]] (the **red** block shown above) are shipped with the program
24
24
- Data in [[#Heap Segment]] (the **blue** block shown above) and [[#Stack Segment]] (the **green** block shown above) are allocated dynamically when the program is running
25
25
26
-
### Heap Segment
26
+
## Heap Segment
27
+
---
27
28
-**Dynamically allocated region** where the [[Process (进程)]] can create new data structures as needed
28
29
-**Grows** and **shrinks** as the **process** allocates and deallocates memory
29
30
@@ -50,33 +51,37 @@ description: Stack (automatic memory management for function variables), Heap (d
50
51
>
51
52
>Refer to this [section of article](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#rust-does-not-permit-manual-memory-management) for more details
52
53
53
-
####Memory leak
54
+
### Memory leak
54
55
- Happens when we **forget to release** data in heap memory using `free()` in the example of C
55
56
- This can eventually lead to the exhaustion of available [[Main Memory]], resulting in **degraded performance** or even **program crashes**
56
-
### Stack Segment
57
+
## Stack Segment
58
+
---
59
+
![[stack_segment.png|300]]
57
60
-**Dynamically allocated region** used to store **function calls**, local variables, and temporary data etc
58
-
- Made up of [[#Stack Frame]], follow a [[Stack]] structure
59
-
-**Grows** as functions are called and **shrinks** as they return
61
+
- Made up of [[#Stack Frame]], following a [[Stack]] structure
62
+
-**Expands** as functions are called and **shrinks** as they return
63
+
- We can obtain the default stack size assigned by the system using `ulimit -s`
60
64
61
65
>[!question]- Grows Downwards
62
-
>Stack Segment starts at a higher [[Memory Address]], then memory address decreases as we add in **Stack Frame**, thus **growing downwards** in terms of Memory Address, so to remove stack frame, we need to increment the [[Register#Stack Pointer]]
66
+
>Stack Segment starts at a higher [[Memory Address]], then **memory address decreases** as we add in **Stack Frame**, thus **growing downwards** in terms of Memory Address, so to remove stack frame, we need to increment the [[Register#Stack Pointer]].
63
67
64
68
>[!info]- Fun Fact Regarding Grow Downwards
65
-
> Growing downwards is a convention from when computers had small memories and the stack was placed at the end of the [[#Data Segment]]. Nowadays the stack can be anywhere, but the convention stuck on, at the end of the day it makes no difference
69
+
> Growing downwards is a convention from when computers had small memories and the stack was placed at the end of the [[#Data Segment]]. Nowadays the stack can be anywhere, but the convention stuck on, at the end of the day it makes no difference.
70
+
71
+
>[!bigbrain] Variables live in the stack
72
+
> When assigning one variable to another variable, data is **duplicated**.
73
+
>
74
+
> For example, `a=1` and `b=a`, the value `1` is duplicated and assigned to `b`. A nice visualisation can be found [here](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#variables-live-in-the-stack)
66
75
67
76
68
-
- When assigning one variable to another variable, data is **duplicated**
69
-
- For example, `a=1``b=a`, the value `1` is duplicated and assigned to `b`
70
-
- A nice visualisation can be found [here](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#variables-live-in-the-stack)
71
77
72
78
>[!success] Data management in Stack Segment is more efficient than Heap Segment
73
79
>1. Stack memory is allocated and deallocated in a **Last In, First Out (LIFO) manner**, making it faster than heap memory. This is because all it needs to do is move the [[Register#Stack Pointer]] up or down, while heap memory requires more complex memory management
74
-
>2. No overhead of complex [[Synchronization (同步)]], unlike data inside heap segment, data inside the stack segment is usually dedicated to that particular[[Process (进程)]] or[[Thread]]. Thus, manipulation of data inside the stack segment doesn't require the complex synchronisation
80
+
>2. No overhead of complex [[Synchronization (同步)]], unlike data inside heap segment, data inside the stack segment is dedicated to that particular [[Thread]]. Thus, manipulation of data inside the stack segment doesn't require the complex synchronisation
75
81
76
-
>[!caution] Stack Overflow
77
-
>Happens when the **size of all the stack frame** is **over** the **default fixed size** of the stack segment
78
82
79
-
>[!caution] Variable Size Defined before Compilation
83
+
>[!caution] Attention
84
+
> The size of variable on the stack is defined before Compilation
80
85
81
86
>[!example]- XV6-RISCV Kernel Stack
82
87
>```c {13}
@@ -133,14 +138,18 @@ description: Stack (automatic memory management for function variables), Heap (d
133
138
>- We load the base address of the **stack segment**
134
139
>- Then based on the core id (0-7), we set the [[Register#Stack Pointer]] for each [[CPU]]. We can see the stack pointer starting point is obtained by adding `(hartid * 4096)` to the base address, this is because **stack segment grows downwards** when we are adding values to the stack
135
140
136
-
#### Stack Frame
141
+
### Stack Frame
137
142
- A section of the [[#Stack Segment]] dedicated to a **specific function call**
138
143
139
-
### Data Segment
144
+
### Stack Overflow
145
+
- Happens when the **size of all the stack frame** is **over** the **default fixed size** of the stack segment
146
+
## Data Segment
147
+
---
140
148
- This region stores **global** and **static variables** and **constants** used by the program, pre-defined before the execution of the program
141
149
- Can be both read and write, allowing the [[Process (进程)]] to manipulate the data as needed
142
150
143
-
### Text Segment
151
+
## Text Segment
152
+
---
144
153
- Stores program codes, **unchangeable**, **read-only**
0 commit comments