shithub: drawcpu

Download patch

ref: f26dd3371d96047be87a41d745dbc73040531a6c
parent: 09a85257b0254babd5e225d00dde49fe63ffb12f
author: halfwit <michaelmisch1985@gmail.com>
date: Thu Jul 24 08:10:44 EDT 2025

Add what mem.h we know well

--- a/main.c
+++ b/main.c
@@ -115,8 +115,8 @@
 		panic("bind #I: %r");
 	if(bind("#U", "/root", MREPL|MCREATE) < 0)
 		panic("bind #U: %r");
-	if(bind("#P", "/proc", MBEFORE) < 0)
-		panic("bind #P: %r");
+	//if(bind("#P", "/proc", MBEFORE) < 0)
+	//	panic("bind #P: %r");
 	bind("#A", "/dev", MAFTER);
 	bind("#N", "/dev", MAFTER);
 	bind("#C", "/", MAFTER);
--- /dev/null
+++ b/posix-386/mem.h
@@ -1,0 +1,172 @@
+/*
+ * Memory and machine-specific definitions.  Used in C and assembler.
+ */
+
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
+
+/*
+ * Sizes
+ */
+#define	BI2BY		8			/* bits per byte */
+#define	BI2WD		32			/* bits per word */
+#define	BY2WD		4			/* bytes per word */
+#define	BY2V		8			/* bytes per double word */
+#define	BY2PG		4096			/* bytes per page */
+#define	WD2PG		(BY2PG/BY2WD)		/* words per page */
+#define	BY2XPG		(4096*1024)		/* bytes per big page */
+#define	PGSHIFT		12			/* log(BY2PG) */
+#define	ROUND(s, sz)	(((s)+((sz)-1))&~((sz)-1))
+#define	PGROUND(s)	ROUND(s, BY2PG)
+#define	BLOCKALIGN	8
+#define	FPalign		16
+
+#define	MAXMACH		32			/* max # cpus system can run */
+#define	KSTACK		4096			/* Size of kernel stack */
+
+/*
+ * Time
+ */
+#define	HZ		(100)			/* clock frequency */
+#define	MS2HZ		(1000/HZ)		/* millisec per clock tick */
+#define	TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
+
+/*
+ *  Address spaces
+ */
+#define	KZERO		0xF0000000		/* base of kernel address space */
+#define	KTZERO		(KZERO+0x100000)	/* first address in kernel text - 9load sits below */
+#define	VPT		(KZERO-VPTSIZE)
+#define	VPTSIZE		BY2XPG
+#define	NVPT		(VPTSIZE/BY2WD)
+#define	KMAP		(VPT-KMAPSIZE)
+#define	KMAPSIZE	BY2XPG
+#define	VMAP		(KMAP-VMAPSIZE)
+#define	VMAPSIZE	(0x10000000-VPTSIZE-KMAPSIZE)
+#define	UZERO		0			/* base of user address space */
+#define	UTZERO		(UZERO+BY2PG)		/* first address in user text */
+#define	USTKTOP		(VMAP-BY2PG)		/* byte just beyond user stack */
+#define	USTKSIZE	(16*1024*1024)		/* size of user stack */
+
+/*
+ * Fundamental addresses
+ */
+#define	CONFADDR	(KZERO+0x1200)		/* info passed from boot loader */
+#define	APBOOTSTRAP	(KZERO+0x7000)		/* AP bootstrap code (overlaps CONFADDR) */
+#define	TMPADDR		(KZERO+0x8000)		/* used for temporary mappings */
+#define	IDTADDR		(KZERO+0x10800)		/* idt */
+#define	REBOOTADDR	(0x11000)		/* reboot code - physical address */
+#define	CPU0PDB		(KZERO+0x12000)		/* bootstrap processor PDB */
+#define	CPU0PTE		(KZERO+0x13000)		/* bootstrap processor PTE's for 0-4MB */
+#define	CPU0PTE1	(KZERO+0x14000)		/* bootstrap processor PTE's for 4-8MB */
+#define	CPU0PTE2	(KZERO+0x15000)		/* bootstrap processor PTE's for 8-12MB */
+#define	CPU0PTE3	(KZERO+0x16000)		/* bootstrap processor PTE's for 12-16MB */
+#define	CPU0GDT		(KZERO+0x17000)		/* bootstrap processor GDT */
+#define	MACHADDR	(KZERO+0x18000)		/* as seen by current processor */
+#define	CPU0MACH	(KZERO+0x19000)		/* Mach for bootstrap processor */
+#define	MACHSIZE	BY2PG
+#define	CPU0END		(CPU0MACH+BY2PG)
+/*
+ * N.B.  ramscan knows that CPU0END is the end of reserved data
+ * N.B.  _startPADDR knows that CPU0PDB is the first reserved page
+ * and that there are 6 of them.
+ */
+
+/*
+ * Where configuration info is left for the loaded programme.
+ * There are 24064 bytes available at CONFADDR.
+ */
+#define BOOTLINE	((char*)CONFADDR)
+#define BOOTLINELEN	64
+#define BOOTARGS	((char*)(CONFADDR+BOOTLINELEN))
+#define BOOTARGSLEN	(0x6000-0x200-BOOTLINELEN)
+
+/*
+ *  known x86 segments (in GDT) and their selectors
+ */
+#define	NULLSEG	0	/* null segment */
+#define	KDSEG	1	/* kernel data/stack */
+#define	KESEG	2	/* kernel executable */	
+#define	UDSEG	3	/* user data/stack */
+#define	UESEG	4	/* user executable */
+#define	TSSSEG	5	/* task segment */
+#define	APMCSEG		6	/* APM code segment */
+#define	APMCSEG16	7	/* APM 16-bit code segment */
+#define	APMDSEG		8	/* APM data segment */
+#define	KESEG16		9	/* kernel executable 16-bit */
+#define	LDTSEG		10	/* local descriptor table */
+#define	PROCSEG0	11	/* per process descriptor0 */
+#define	NPROCSEG	3	/* number of per process descriptors */
+#define	NGDT		14	/* number of GDT entries required */
+
+#define	SELGDT	(0<<2)	/* selector is in gdt */
+#define	SELLDT	(1<<2)	/* selector is in ldt */
+
+#define	SELECTOR(i, t, p)	(((i)<<3) | (t) | (p))
+
+#define	NULLSEL	SELECTOR(NULLSEG, SELGDT, 0)
+#define	KDSEL	SELECTOR(KDSEG, SELGDT, 0)
+#define	KESEL	SELECTOR(KESEG, SELGDT, 0)
+#define	UESEL	SELECTOR(UESEG, SELGDT, 3)
+#define	UDSEL	SELECTOR(UDSEG, SELGDT, 3)
+#define	TSSSEL	SELECTOR(TSSSEG, SELGDT, 0)
+#define	APMCSEL 	SELECTOR(APMCSEG, SELGDT, 0)
+#define	APMCSEL16	SELECTOR(APMCSEG16, SELGDT, 0)
+#define	APMDSEL		SELECTOR(APMDSEG, SELGDT, 0)
+#define	LDTSEL	SELECTOR(LDTSEG, SELGDT, 0)
+
+/*
+ *  fields in segment descriptors
+ */
+#define	SEGDATA	(0x10<<8)	/* data/stack segment */
+#define	SEGEXEC	(0x18<<8)	/* executable segment */
+#define	SEGTSS	(0x9<<8)	/* TSS segment */
+#define	SEGCG	(0x0C<<8)	/* call gate */
+#define	SEGIG	(0x0E<<8)	/* interrupt gate */
+#define	SEGTG	(0x0F<<8)	/* trap gate */
+#define	SEGLDT	(0x02<<8)	/* local descriptor table */
+#define	SEGTYPE	(0x1F<<8)
+
+#define	SEGP	(1<<15)		/* segment present */
+#define	SEGPL(x) ((x)<<13)	/* priority level */
+#define	SEGB	(1<<22)		/* granularity 1==4k (for expand-down) */
+#define	SEGG	(1<<23)		/* granularity 1==4k (for other) */
+#define	SEGE	(1<<10)		/* expand down */
+#define	SEGW	(1<<9)		/* writable (for data/stack) */
+#define	SEGR	(1<<9)		/* readable (for code) */
+#define	SEGD	(1<<22)		/* default 1==32bit (for code) */
+
+/*
+ *  virtual MMU
+ */
+#define	PTEMAPMEM	(1024*1024)	
+#define	PTEPERTAB	(PTEMAPMEM/BY2PG)
+#define	SEGMAPSIZE	1984
+#define	SSEGMAPSIZE	16
+#define	PPN(x)		((x)&~(BY2PG-1))
+
+/*
+ *  physical MMU
+ */
+#define	PTEVALID	(1<<0)
+#define	PTEWT		(1<<3)
+#define	PTEUNCACHED	(1<<4)
+#define	PTECACHED	(0<<4)
+#define	PTEWRITE	(1<<1)
+#define	PTERONLY	(0<<1)
+#define	PTEKERNEL	(0<<2)
+#define	PTEUSER		(1<<2)
+#define	PTESIZE		(1<<7)
+#define	PTEGLOBAL	(1<<8)
+
+/*
+ * Macros for calculating offsets within the page directory base
+ * and page tables. 
+ */
+#define	PDX(va)		((((ulong)(va))>>22) & 0x03FF)
+#define	PTX(va)		((((ulong)(va))>>12) & 0x03FF)
+
+#define	getpgcolor(a)	0
+
+/* PAT entry used for write combining */
+#define PATWC	7
\ No newline at end of file
--- /dev/null
+++ b/posix-amd64/mem.h
@@ -1,0 +1,185 @@
+/*
+ * Memory and machine-specific definitions.  Used in C and assembler.
+ */
+#define KiB		1024u			/* Kibi 0x0000000000000400 */
+#define MiB		1048576u		/* Mebi 0x0000000000100000 */
+#define GiB		1073741824u		/* Gibi 000000000040000000 */
+#define TiB		1099511627776ull	/* Tebi 0x0000010000000000 */
+#define PiB		1125899906842624ull	/* Pebi 0x0004000000000000 */
+#define EiB		1152921504606846976ull	/* Exbi 0x1000000000000000 */
+
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
+
+#define ALIGNED(p, a)	(!(((uintptr)(p)) & ((a)-1)))
+
+/*
+ * Sizes
+ */
+#define	BI2BY		8			/* bits per byte */
+#define	BI2WD		32			/* bits per word */
+#define	BY2WD		8			/* bytes per word */
+#define	BY2V		8			/* bytes per double word */
+#define	BY2PG		(0x1000ull)		/* bytes per page */
+#define	WD2PG		(BY2PG/BY2WD)		/* words per page */
+#define	PGSHIFT		12			/* log(BY2PG) */
+#define	ROUND(s, sz)	(((s)+((sz)-1))&~((sz)-1))
+#define	PGROUND(s)	ROUND(s, BY2PG)
+#define	BLOCKALIGN	8
+#define	FPalign		64
+
+#define	MAXMACH		128			/* max # cpus system can run */
+
+#define KSTACK		(8*KiB)			/* Size of Proc kernel stack */
+
+/*
+ * Time
+ */
+#define HZ		(100)			/* clock frequency */
+#define MS2HZ		(1000/HZ)		/* millisec per clock tick */
+#define TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
+
+/*
+ *  Address spaces. User:
+ */
+#define UTZERO		(0x0000000000200000ull)		/* first address in user text */
+#define UADDRMASK	(0x00007fffffffffffull)		/* canonical address mask */
+#define USTKTOP		(0x00007ffffffff000ull)
+#define USTKSIZE	(16*MiB)			/* size of user stack */
+
+/*
+ *  Address spaces. Kernel, sorted by address.
+ */
+#define KZERO		(0xffffffff80000000ull)
+#define KTZERO		(KZERO+1*MiB+64*KiB)
+
+#define VMAP		(0xfffffe8000000000ull)
+#define VMAPSIZE	(1024ull*GiB)
+
+#define	KMAP		(0xfffffe0000000000ull)
+#define KMAPSIZE	(2*MiB)
+
+/*
+ * Fundamental addresses
+ */
+#define	CONFADDR	(KZERO+0x1200ull)		/* info passed from boot loader */
+#define	APBOOTSTRAP	(KZERO+0x7000ull)		/* AP bootstrap code */
+#define	IDTADDR		(KZERO+0x10000ull)		/* idt */
+#define	REBOOTADDR	(0x11000)			/* reboot code - physical address */
+
+#define CPU0PML4	(KZERO+0x13000ull)
+#define CPU0PDP		(KZERO+0x14000ull)
+#define CPU0PD0		(KZERO+0x15000ull)		/* KZERO */
+#define CPU0PD1		(KZERO+0x16000ull)		/* KZERO+1GB */
+
+#define	CPU0GDT		(KZERO+0x17000ull)		/* bootstrap processor GDT */
+
+#define	CPU0MACH	(KZERO+0x18000ull)		/* Mach for bootstrap processor */
+#define CPU0END		(CPU0MACH+MACHSIZE)
+
+#define	MACHSIZE	(2*KSTACK)
+
+/*
+ * Where configuration info is left for the loaded programme.
+ * There are 24064 bytes available at CONFADDR.
+ */
+#define BOOTLINE	((char*)CONFADDR)
+#define BOOTLINELEN	64
+#define BOOTARGS	((char*)(CONFADDR+BOOTLINELEN))
+#define BOOTARGSLEN	(0x6000-0x200-BOOTLINELEN)
+
+/*
+ *  known x86 segments (in GDT) and their selectors
+ */
+#define	NULLSEG	0	/* null segment */
+#define	KESEG	1	/* kernel executable */
+#define KDSEG	2	/* kernel data */
+#define UE32SEG	3	/* user executable 32bit */
+#define	UDSEG	4	/* user data/stack */
+#define	UESEG	5	/* user executable 64bit */
+#define	TSSSEG	8	/* task segment (two descriptors) */
+
+#define	NGDT	10	/* number of GDT entries required */
+
+#define	SELGDT	(0<<2)	/* selector is in gdt */
+#define	SELLDT	(1<<2)	/* selector is in ldt */
+
+#define	SELECTOR(i, t, p)	(((i)<<3) | (t) | (p))
+
+#define	NULLSEL	SELECTOR(NULLSEG, SELGDT, 0)
+#define KDSEL	NULLSEL
+#define	KESEL	SELECTOR(KESEG, SELGDT, 0)
+#define	UE32SEL	SELECTOR(UE32SEG, SELGDT, 3)
+#define	UDSEL	SELECTOR(UDSEG, SELGDT, 3)
+#define	UESEL	SELECTOR(UESEG, SELGDT, 3)
+#define	TSSSEL	SELECTOR(TSSSEG, SELGDT, 0)
+
+/*
+ *  fields in segment descriptors
+ */
+#define	SEGDATA	(0x10<<8)	/* data/stack segment */
+#define	SEGEXEC	(0x18<<8)	/* executable segment */
+#define	SEGTSS	(0x9<<8)	/* TSS segment */
+#define	SEGCG	(0x0C<<8)	/* call gate */
+#define	SEGIG	(0x0E<<8)	/* interrupt gate */
+#define	SEGTG	(0x0F<<8)	/* trap gate */
+#define	SEGLDT	(0x02<<8)	/* local descriptor table */
+#define	SEGTYPE	(0x1F<<8)
+
+#define	SEGP	(1<<15)		/* segment present */
+#define	SEGPL(x) ((x)<<13)	/* priority level */
+#define	SEGB	(1<<22)		/* granularity 1==4k (for expand-down) */
+#define	SEGD	(1<<22)		/* default 1==32bit (for code) */
+#define	SEGE	(1<<10)		/* expand down */
+#define	SEGW	(1<<9)		/* writable (for data/stack) */
+#define	SEGR	(1<<9)		/* readable (for code) */
+#define SEGL	(1<<21)		/* 64 bit */
+#define	SEGG	(1<<23)		/* granularity 1==4k (for other) */
+
+/*
+ *  virtual MMU
+ */
+#define	PTEMAPMEM	(1ull*MiB)	
+#define	PTEPERTAB	(PTEMAPMEM/BY2PG)
+#define	SEGMAPSIZE	65536
+#define	SSEGMAPSIZE	16
+#define	PPN(x)		((x)&~(1ull<<63 | BY2PG-1))
+
+/*
+ *  physical MMU
+ */
+#define	PTEVALID	(1ull<<0)
+#define	PTEWT		(1ull<<3)
+#define	PTEUNCACHED	(1ull<<4)
+#define	PTECACHED	(0ull<<4)
+#define	PTEWRITE	(1ull<<1)
+#define	PTERONLY	(0ull<<1)
+#define	PTEKERNEL	(0ull<<2)
+#define	PTEUSER		(1ull<<2)
+#define	PTEACCESSED	(1ull<<5)
+#define	PTEDIRTY	(1ull<<6)
+#define	PTESIZE		(1ull<<7)
+#define	PTEGLOBAL	(1ull<<8)
+#define	PTENOEXEC	((uvlong)m->havenx<<63)
+
+/*
+ * Hierarchical Page Tables.
+ * For example, traditional IA-32 paging structures have 2 levels,
+ * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
+ * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
+ * and level 0 the PT pages. The PTLX macro gives an index into the
+ * page-table page at level 'l' for the virtual address 'v'.
+ */
+#define PTSZ		(4*KiB)			/* page table page size */
+#define PTSHIFT		9			/*  */
+
+#define PTLX(v, l)	(((v)>>(((l)*PTSHIFT)+PGSHIFT)) & ((1<<PTSHIFT)-1))
+#define PGLSZ(l)	(1ull<<(((l)*PTSHIFT)+PGSHIFT))
+
+#define	getpgcolor(a)	0
+
+/* PAT entry used for write combining */
+#define PATWC	7
+
+#define RMACH		R15			/* m-> */
+#define RUSER		R14			/* up-> */
\ No newline at end of file
--- /dev/null
+++ b/posix-arm/mem.h
@@ -1,0 +1,100 @@
+/*
+ * Memory and machine-specific definitions.  Used in C and assembler.
+ */
+#define KiB		1024u			/* Kibi 0x0000000000000400 */
+#define MiB		1048576u		/* Mebi 0x0000000000100000 */
+#define GiB		1073741824u		/* Gibi 000000000040000000 */
+
+/*
+ * Sizes
+ */
+#define	BY2PG		(4*KiB)			/* bytes per page */
+#define	PGSHIFT		12			/* log(BY2PG) */
+#define	PGROUND(s)	ROUND(s, BY2PG)
+#define	ROUND(s, sz)	(((s)+(sz-1))&~(sz-1))
+
+#define	MAXMACH		4			/* max # cpus system can run */
+#define	MACHSIZE	BY2PG
+#define L1SIZE		(4 * BY2PG)
+
+#define KSTACK		(8*KiB)
+#define STACKALIGN(sp)	((sp) & ~3)		/* bug: assure with alloc */
+
+/*
+ * Magic registers
+ */
+
+#define	USER		9		/* R9 is up-> */
+#define	MACH		10		/* R10 is m-> */
+
+/*
+ * Address spaces.
+ * KTZERO is used by kprof and dumpstack (if any).
+ *
+ * KZERO is mapped to physical 0 (start of ram).
+ *
+ * vectors are at 0, plan9.ini is at KZERO+256 and is limited to 16K by
+ * devenv.
+ */
+
+#define	KSEG0		0x80000000		/* kernel segment */
+/* mask to check segment; good for 1GB dram */
+#define	KSEGM		0xC0000000
+#define	KZERO		KSEG0			/* kernel address space */
+#define CONFADDR	(KZERO+0x100)		/* unparsed plan9.ini */
+#define	REBOOTADDR	(0x1c00)		/* reboot code - physical address */
+#define	MACHADDR	(KZERO+0x2000)		/* Mach structure */
+#define	L2		(KZERO+0x3000)		/* L2 ptes for vectors etc */
+#define	VCBUFFER	(KZERO+0x3400)		/* videocore mailbox buffer */
+#define	FIQSTKTOP	(KZERO+0x4000)		/* FIQ stack */
+#define	L1		(KZERO+0x4000)		/* tt ptes: 16KiB aligned */
+#define	KTZERO		(KZERO+0x8000)		/* kernel text start */
+#define VIRTIO		(0x7E000000)		/* i/o registers */
+#define	ARMLOCAL	(0x7F000000)		/* armv7 only */
+#define	VGPIO		(ARMLOCAL+MiB)		/* virtual gpio for pi3 ACT LED */
+#define	FRAMEBUFFER	0xC0000000		/* video framebuffer */
+
+#define	UZERO		0			/* user segment */
+#define	UTZERO		(UZERO+BY2PG)		/* user text start */
+#define	USTKTOP		0x40000000		/* user segment end +1 */
+#define	USTKSIZE	(8*1024*1024)		/* user stack size */
+
+/*
+ * Legacy...
+ */
+#define BLOCKALIGN	64			/* only used in allocb.c */
+
+/*
+ * Sizes
+ */
+#define BI2BY		8			/* bits per byte */
+#define BY2SE		4
+#define BY2WD		4
+#define BY2V		8			/* only used in xalloc.c */
+
+#define	PTEMAPMEM	(1024*1024)
+#define	PTEPERTAB	(PTEMAPMEM/BY2PG)
+#define	SEGMAPSIZE	1984
+#define	SSEGMAPSIZE	16
+#define	PPN(x)		((x)&~(BY2PG-1))
+
+/*
+ * These bits are completely artificial.
+ * With a little work these move to port.
+ */
+#define	PTEVALID	(1<<0)
+#define	PTERONLY	0
+#define	PTEWRITE	(1<<1)
+#define	PTECACHED	0
+#define	PTEUNCACHED	(1<<2)
+#define	PTENOEXEC	(1<<4)
+
+/*
+ * Physical machine information from here on.
+ *	PHYS addresses as seen from the arm cpu.
+ *	BUS  addresses as seen from the videocore gpu.
+ */
+#define	PHYSDRAM	0
+
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
\ No newline at end of file
--