First, it's important to note the difference between kernel<->userspace switch and kernel process -> another kernel process switch. The kernel<->userspace switch is called traps (syscalls, interrupts, etc), and kernel process -> another kernel process is called context switching.
How kernel<->userspace works
1. Userspace makes syscall (mov rax, syscall num; syscall)
2. Syscall instruction finds the interrupt table (in xk: vectors.S), jumps to the correct entry. In the case of a syscall, it's entry 0x40, but for example, timer interrupts will have a different value for this. Additionally, syscall instruction switches CPU into kernel mode, and saves rip,rsp,eflags onto kernel stack.
3. The interrupt handler pushes 0 (some systems use this as the error code, ignore this), then the interrupt number (so in the case of syscall, 0x40).
4. Interrupt handler jumps to alltraps.
5. Alltraps saves rest of registers onto kernel stack, then jumps to trap(tf) with the trapframe (the set of all saved registers) as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adj list, edge list, adj matrix | |
# in an adj list, each vertex contains a list of nodes it is adjacent to | |
# simple undirected, unweighted graph using adjacency list representation | |
class Graph: | |
def __init__(self): | |
self.graph = dict() | |
def add_node(self, label): | |
assert label not in self.graph | |
self.graph[label] = list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ID: daniel20 | |
LANG: PYTHON3 | |
TASK: castle | |
''' | |
global WEST, NORTH, EAST, SOUTH | |
WEST, NORTH, EAST, SOUTH = 0, 1, 2, 3 | |
def closed(room, direction=NORTH): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# simple trie implementation | |
class Trie: | |
class Node: | |
def __init__(self, data=None, end=False): | |
self.data = data | |
self.children = dict() | |
# is this the end of a word? | |
self.end = end | |
def __str__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pwn import * | |
import struct | |
def p64(x): | |
return struct.pack('Q', x) | |
def u64(x): | |
return struct.unpack('Q', x)[0] | |
def p32(x): | |
return struct.pack('<I', x) | |
def u32(x): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def str2bytes(s): | |
ret = [] | |
for i in range(0, len(s), 8): | |
sub = s[i:i+8][::-1] | |
cur = 0 | |
for x in sub: | |
cur <<= 8 | |
cur |= ord(x) | |
ret.append(cur) | |
return ret[::-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
x=list(map(lambda x: x[:-4], os.listdir('/usr/share/cowsay/cows/'))) | |
cf=x[random.randint(0,len(x)-1)] | |
c='cowsay' if random.random() < 0.5 else 'cowthink' | |
os.system(f'fortune -a | {c} -f {cf}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef UNICODE | |
#define UNICODE | |
#endif | |
#define PIXELDATA_SIZE 90000 | |
#include <windows.h> | |
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); |