131 Week 11/12 - Memory layout Flashcards
64 bit processors
In theory they can address 2^64 bytes of byte addressable memory but currently all addresses are not needed to be used
Virtual memory
Read/write operations use virtual memory addresses.
Memory management unit (MMU) in microprocessor uses a lookup table called translation lookaside buffer (TLB) to translate virtual
to physical addresses.
OS dynamically allocates pages (4KB per page for x86 architecture) to process and create entries in TLB.
Virtual memory layout
//high address
- OS Kernel: Memory reserved by the OS to monitor and control mapping between physical and virtual addresses.
- STACK: Data needed by function calls, including arguments and local variables. The set of values pushed for one function call is referred to as a stack frame.
- HEAP: Dynamic memory allocation for variables whose size is known at runtime and cannot be statically determined by the compiler before program execution (e.g., malloc, new, free, delete).
- BSS Global variables that are initialized to zero or do not have explicit initialization.
- DATA: Global variables that have been initialized.
- TEXT: The binary executable instructions of a program.
//low address
Stack and heap will grow from the top and bottom of the unused memory space respectively.
Stack pointers
Stack pointer (SP): points to the lowest address of the stack for the current frame.
Frame pointer (FP)/ base pointer (BP): points to a reference address of the previous frame.
Saved instruction pointer (IP): points to the address that the function will return to.
Stack layout
Stack is made up of frames.
Frame is made up of:
- function parameters
- saved instruction pointer (IP)
- saved base pointer (BP)
- locally declared variables
Parameter values are pushed in reverse order, so that the first parameter will be the last to be pushed to the stack.
Each function call will have its own stack frame in the stack. Frames will be removed from the stack when the function has fully executed.
Examining the stack
- Use lscpu to find if CPU architecture is little or big endian.
- Develop code to examine
- Use -g flag to compile to include debug info in executable
- Set breakpoints for when you want to examine the stack. Run the code using run. Use info stack at breakpoints to display the call stack
- Can use info frame int to examine a specific frame where int is a number referring to a stack frame.
- Can use info reg to print contents of registers.
- Can use disass frameName to see contents of a frame
Visualising memory
can use command x/intcharx address to examine memory.
char specifies the type to examine
int specifies how many of the types are examined
address is the starting address to be examined from
e.g., x/28xw $sp examines 28 words from the address of stack pointer.
x/40xb &s examines 40 bytes from the address of s
Flash memory
Non-volatile memory. Not as fast as RAM but faster than hard disks.
Random access memory (RAM)
Volatile memory that can be static (SRAM – used for the cache of the CPU to store common instructions) or dynamic (DRAM – used as the main memory).
nRF52833 System on Chip memory map
Device: 1.5GB
RAM: 1GB
Peripheral
SRAM
Code: 512MB
memory mapped IO
Allows microcontroller to interact with peripherals and devices (general purpose IO - GPIO) by reading from and writing to predefined memory addresses.
nRF52833 System on Chip memory map when Bluetooth is disabled
SRAM {
RAM, 127.98 KB
NONIT, 16B: Memory area that persists across micro:bit resets. E.g., tracking how many time reset has been pressed so Bluetooth pairing can be activated.
Code {
UCIR, 8B: UICR: User Information Configuration Register (reserved).
Storage, 4KB
Flash, 508KB: Long term non-volatile data (e.g., calibration data for the sensors). }
RAM stores stack, heap, BSS, data. Flash stores text
nRF52833 System on Chip memory map when Bluetooth is enabled
SRAM {
Same for if Bluetooth is enabled or disabled. }
Code {
Settings, 8KB: Holds similar information to ‘Storage’ as well as Bluetooth pairing keys and state.
Bootloader, 28KB: Memory area for over-the-air firmware updates.
SD, 108KB: soft device, area that holds information about the Bluetooth low energy (BLE) protocol.
MBR, 4KB: Master Boot Record
RAM stores stack, heap, BSS, data. Flash stores text.
Text size is decreased when Bluetooth is enabled so code cannot be as large.