No more intel/minix segments.

This commit removes all traces of Minix segments (the text/data/stack
memory map abstraction in the kernel) and significance of Intel segments
(hardware segments like CS, DS that add offsets to all addressing before
page table translation). This ultimately simplifies the memory layout
and addressing and makes the same layout possible on non-Intel
architectures.

There are only two types of addresses in the world now: virtual
and physical; even the kernel and processes have the same virtual
address space. Kernel and user processes can be distinguished at a
glance as processes won't use 0xF0000000 and above.

No static pre-allocated memory sizes exist any more.

Changes to booting:
        . The pre_init.c leaves the kernel and modules exactly as
          they were left by the bootloader in physical memory
        . The kernel starts running using physical addressing,
          loaded at a fixed location given in its linker script by the
          bootloader.  All code and data in this phase are linked to
          this fixed low location.
        . It makes a bootstrap pagetable to map itself to a
          fixed high location (also in linker script) and jumps to
          the high address. All code and data then use this high addressing.
        . All code/data symbols linked at the low addresses is prefixed by
          an objcopy step with __k_unpaged_*, so that that code cannot
          reference highly-linked symbols (which aren't valid yet) or vice
          versa (symbols that aren't valid any more).
        . The two addressing modes are separated in the linker script by
          collecting the unpaged_*.o objects and linking them with low
          addresses, and linking the rest high. Some objects are linked
          twice, once low and once high.
        . The bootstrap phase passes a lot of information (e.g. free memory
          list, physical location of the modules, etc.) using the kinfo
          struct.
        . After this bootstrap the low-linked part is freed.
        . The kernel maps in VM into the bootstrap page table so that VM can
          begin executing. Its first job is to make page tables for all other
          boot processes. So VM runs before RS, and RS gets a fully dynamic,
          VM-managed address space. VM gets its privilege info from RS as usual
          but that happens after RS starts running.
        . Both the kernel loading VM and VM organizing boot processes happen
	  using the libexec logic. This removes the last reason for VM to
	  still know much about exec() and vm/exec.c is gone.

Further Implementation:
        . All segments are based at 0 and have a 4 GB limit.
        . The kernel is mapped in at the top of the virtual address
          space so as not to constrain the user processes.
        . Processes do not use segments from the LDT at all; there are
          no segments in the LDT any more, so no LLDT is needed.
        . The Minix segments T/D/S are gone and so none of the
          user-space or in-kernel copy functions use them. The copy
          functions use a process endpoint of NONE to realize it's
          a physical address, virtual otherwise.
        . The umap call only makes sense to translate a virtual address
          to a physical address now.
        . Segments-related calls like newmap and alloc_segments are gone.
        . All segments-related translation in VM is gone (vir2map etc).
        . Initialization in VM is simpler as no moving around is necessary.
        . VM and all other boot processes can be linked wherever they wish
          and will be mapped in at the right location by the kernel and VM
          respectively.

Other changes:
        . The multiboot code is less special: it does not use mb_print
          for its diagnostics any more but uses printf() as normal, saving
          the output into the diagnostics buffer, only printing to the
          screen using the direct print functions if a panic() occurs.
        . The multiboot code uses the flexible 'free memory map list'
          style to receive the list of free memory if available.
        . The kernel determines the memory layout of the processes to
          a degree: it tells VM where the kernel starts and ends and
          where the kernel wants the top of the process to be. VM then
          uses this entire range, i.e. the stack is right at the top,
          and mmap()ped bits of memory are placed below that downwards,
          and the break grows upwards.

Other Consequences:
        . Every process gets its own page table as address spaces
          can't be separated any more by segments.
        . As all segments are 0-based, there is no distinction between
          virtual and linear addresses, nor between userspace and
          kernel addresses.
        . Less work is done when context switching, leading to a net
          performance increase. (8% faster on my machine for 'make servers'.)
	. The layout and configuration of the GDT makes sysenter and syscall
	  possible.
This commit is contained in:
Ben Gras
2012-07-15 22:30:15 +02:00
parent cfe1ed4df4
commit 50e2064049
139 changed files with 2468 additions and 4380 deletions
+5 -10
View File
@@ -14,24 +14,19 @@ struct segdesc_s { /* segment descriptor for protected mode */
u8_t access; /* |P|DL|1|X|E|R|A| */
u8_t granularity; /* |G|X|0|A|LIMT| */
u8_t base_high;
};
} __attribute__((packed));
#define LDT_SIZE 2 /* CS and DS */
/* Fixed local descriptors. */
#define CS_LDT_INDEX 0 /* process CS */
#define DS_LDT_INDEX 1 /* process DS=ES=FS=GS=SS */
struct desctableptr_s {
u16_t limit;
u32_t base;
} __attribute__((packed));
typedef struct segframe {
reg_t p_ldt_sel; /* selector in gdt with ldt base and limit */
reg_t p_cr3; /* page table root */
u32_t *p_cr3_v;
char *fpu_state;
struct segdesc_s p_ldt[LDT_SIZE]; /* CS, DS and remote */
} segframe_t;
#define INMEMORY(p) (!p->p_seg.p_cr3 || get_cpulocal_var(ptproc) == p)
typedef u32_t atomic_t; /* access to an aligned 32bit value is atomic on i386 */
#endif /* #ifndef _I386_TYPES_H */
+5 -14
View File
@@ -14,6 +14,9 @@
/* Magic numbers for interrupt controller. */
#define END_OF_INT 0x20 /* code used to re-enable after an interrupt */
#define IRQ0_VECTOR 0x50 /* nice vectors to relocate IRQ0-7 to */
#define IRQ8_VECTOR 0x70 /* no need to move IRQ8-15 */
/* Interrupt vectors defined/reserved by processor. */
#define DIVIDE_VECTOR 0 /* divide error */
#define DEBUG_VECTOR 1 /* single step (trace) */
@@ -25,15 +28,6 @@
#define KERN_CALL_VECTOR 32 /* system calls are made with int SYSVEC */
#define IPC_VECTOR 33 /* interrupt vector for ipc */
/* Suitable irq bases for hardware interrupts. Reprogram the 8259(s) from
* the PC BIOS defaults since the BIOS doesn't respect all the processor's
* reserved vectors (0 to 31).
*/
#define BIOS_IRQ0_VEC 0x08 /* base of IRQ0-7 vectors used by BIOS */
#define BIOS_IRQ8_VEC 0x70 /* base of IRQ8-15 vectors used by BIOS */
#define IRQ0_VECTOR 0x50 /* nice vectors to relocate IRQ0-7 to */
#define IRQ8_VECTOR 0x70 /* no need to move IRQ8-15 */
/* Hardware interrupt numbers. */
#ifndef USE_APIC
#define NR_IRQ_VECTORS 16
@@ -55,11 +49,8 @@
#define AT_WINI_0_IRQ 14 /* at winchester controller 0 */
#define AT_WINI_1_IRQ 15 /* at winchester controller 1 */
/* Interrupt number to hardware vector. */
#define BIOS_VECTOR(irq) \
(((irq) < 8 ? BIOS_IRQ0_VEC : BIOS_IRQ8_VEC) + ((irq) & 0x07))
#define VECTOR(irq) \
(((irq) < 8 ? IRQ0_VECTOR : IRQ8_VECTOR) + ((irq) & 0x07))
#define VECTOR(irq) \
(((irq) < 8 ? IRQ0_VECTOR : IRQ8_VECTOR) + ((irq) & 0x07))
#endif /* (CHIP == INTEL) */
+21 -6
View File
@@ -14,8 +14,6 @@
#define MULTIBOOT_AOUT_KLUDGE 0x00010000
#define MULTIBOOT_FLAGS (MULTIBOOT_MEMORY_INFO | MULTIBOOT_PAGE_ALIGN)
/* consts used for Multiboot pre-init */
#define MULTIBOOT_VIDEO_MODE_EGA 1
@@ -28,13 +26,18 @@
#define MULTIBOOT_CONSOLE_LINES 25
#define MULTIBOOT_CONSOLE_COLS 80
#define MULTIBOOT_VIDEO_BUFFER_BYTES \
(MULTIBOOT_CONSOLE_LINES*MULTIBOOT_CONSOLE_COLS*2)
#define MULTIBOOT_STACK_SIZE 4096
#define MULTIBOOT_PARAM_BUF_SIZE 1024
#define MULTIBOOT_MAX_MODS 20
/* Flags to be set in the ’flags’ member of the multiboot info structure. */
#define MULTIBOOT_INFO_MEMORY 0x00000001
#define MULTIBOOT_INFO_MEM_MAP 0x00000040
/* Is there a boot device set? */
#define MULTIBOOT_INFO_BOOTDEV 0x00000002
@@ -45,6 +48,8 @@
/* Are there modules to do something with? */
#define MULTIBOOT_INFO_MODS 0x00000008
#define MULTIBOOT_HIGH_MEM_BASE 0x100000
#ifndef __ASSEMBLY__
#include <minix/types.h>
@@ -73,8 +78,8 @@ struct multiboot_info
/* Multiboot info version number */
u32_t flags;
/* Available memory from BIOS */
u32_t mem_lower;
u32_t mem_upper;
u32_t mem_lower_unused; /* minix uses memmap instead */
u32_t mem_upper_unused;
/* "root" partition */
u32_t boot_device;
/* Kernel command line */
@@ -121,8 +126,18 @@ struct multiboot_mod_list
};
typedef struct multiboot_mod_list multiboot_module_t;
/* Buffer for multiboot parameters */
extern char multiboot_param_buf[];
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
struct multiboot_mmap_entry
{
u32_t size;
u64_t addr;
u64_t len;
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
u32_t type;
} __attribute__((packed));
typedef struct multiboot_mmap_entry multiboot_memory_map_t;
#endif /* __ASSEMBLY__ */
#endif /* __MULTIBOOT_H__ */
-5
View File
@@ -49,9 +49,4 @@
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define PAGE_MASK (PAGE_SIZE - 1)
/* As visible from the user space process, where is the top of the
* stack (first non-stack byte), when in paged mode?
*/
#define VM_STACKTOP 0x80000000
#endif /* _I386_VMPARAM_H_ */
+3 -12
View File
@@ -89,9 +89,6 @@
#define ROOT_SYS_PROC_NR RS_PROC_NR
#define ROOT_USR_PROC_NR INIT_PROC_NR
/* Number of processes contained in the system image. */
#define NR_BOOT_PROCS (NR_TASKS + LAST_SPECIAL_PROC_NR + 1)
/*===========================================================================*
* Kernel notification types *
*===========================================================================*/
@@ -307,7 +304,6 @@
# define SYS_SIGSEND (KERNEL_CALL + 9) /* sys_sigsend() */
# define SYS_SIGRETURN (KERNEL_CALL + 10) /* sys_sigreturn() */
# define SYS_NEWMAP (KERNEL_CALL + 11) /* sys_newmap() */
# define SYS_MEMSET (KERNEL_CALL + 13) /* sys_memset() */
# define SYS_UMAP (KERNEL_CALL + 14) /* sys_umap() */
@@ -533,16 +529,13 @@
#define SIG_MAP m2_l1 /* used by kernel to pass signal bit map */
#define SIG_CTXT_PTR m2_p1 /* pointer to info to restore signal context */
/* Field names for SYS_FORK, _EXEC, _EXIT, _NEWMAP, GETMCONTEXT, SETMCONTEXT.*/
/* Field names for SYS_FORK, _EXEC, _EXIT, GETMCONTEXT, SETMCONTEXT.*/
#define PR_ENDPT m1_i1 /* indicates a process */
#define PR_PRIORITY m1_i2 /* process priority */
#define PR_SLOT m1_i2 /* indicates a process slot */
#define PR_STACK_PTR m1_p1 /* used for stack ptr in sys_exec, sys_getsp */
#define PR_NAME_PTR m1_p2 /* tells where program name is for dmp */
#define PR_IP_PTR m1_p3 /* initial value for ip after exec */
#define PR_MEM_PTR m1_p1 /* tells where memory map is for sys_newmap
* and sys_fork
*/
#define PR_FORK_FLAGS m1_i3 /* optional flags for fork operation */
#define PR_FORK_MSGADDR m1_p1 /* reply message address of forked child */
#define PR_CTX_PTR m1_p1 /* pointer to mcontext_t structure */
@@ -624,11 +617,8 @@
#define VMCTL_I386_GETCR3 13
#define VMCTL_MEMREQ_GET 14
#define VMCTL_MEMREQ_REPLY 15
#define VMCTL_INCSP 16
#define VMCTL_NOPAGEZERO 18
#define VMCTL_I386_KERNELLIMIT 19
#define VMCTL_I386_FREEPDE 23
#define VMCTL_ENABLE_PAGING 24
#define VMCTL_I386_INVLPG 25
#define VMCTL_FLUSHTLB 26
#define VMCTL_KERN_PHYSMAP 27
@@ -636,6 +626,7 @@
#define VMCTL_SETADDRSPACE 29
#define VMCTL_VMINHIBIT_SET 30
#define VMCTL_VMINHIBIT_CLEAR 31
#define VMCTL_CLEARMAPCACHE 32
/* Codes and field names for SYS_SYSCTL. */
#define SYSCTL_CODE m1_i1 /* SYSCTL_CODE_* below */
@@ -830,7 +821,7 @@
/* Parameters for the EXEC_NEWMEM call */
#define EXC_NM_PROC m1_i1 /* process that needs new map */
#define EXC_NM_PTR m1_p1 /* parameters in struct exec_newmem */
#define EXC_NM_PTR m1_p1 /* parameters in struct exec_info */
/* Results:
* the status will be in m_type.
* the top of the stack will be in m1_i1.
+6 -10
View File
@@ -57,18 +57,14 @@
#define SEGMENT_TYPE 0xFF00 /* bit mask to get segment type */
#define SEGMENT_INDEX 0x00FF /* bit mask to get segment index */
#define LOCAL_SEG 0x0000 /* flags indicating local memory segment */
#define NR_LOCAL_SEGS 3 /* # local segments per process (fixed) */
#define T 0 /* proc[i].mem_map[T] is for text */
#define D 1 /* proc[i].mem_map[D] is for data */
#define S 2 /* proc[i].mem_map[S] is for stack */
#define D_OBSOLETE 1 /* proc[i].mem_map[D] is for data */
#define PHYS_SEG 0x0400 /* flag indicating entire physical memory */
#define LOCAL_VM_SEG 0x1000 /* same as LOCAL_SEG, but with vm lookup */
#define VM_D (LOCAL_VM_SEG | D)
#define VM_T (LOCAL_VM_SEG | T)
#define MEM_GRANT 3
#define VIR_ADDR 1
#define VM_D (LOCAL_VM_SEG | VIR_ADDR)
#define VM_GRANT (LOCAL_VM_SEG | MEM_GRANT)
/* Labels used to disable code sections for different reasons. */
@@ -76,6 +72,9 @@
#define FUTURE_CODE 0 /* new code to be activated + tested later */
#define TEMP_CODE 1 /* active code to be removed later */
/* Number of processes contained in the system image. */
#define NR_BOOT_PROCS (NR_TASKS + LAST_SPECIAL_PROC_NR + 1)
/* Process name length in the PM process table, including '\0'. */
#define PROC_NAME_LEN 16
@@ -157,9 +156,6 @@
#define SERVARNAME "cttyline"
#define SERBAUDVARNAME "cttybaud"
/* Bits for the system property flags in boot image processes. */
#define PROC_FULLVM 0x100 /* VM sets and manages full pagetable */
/* Bits for s_flags in the privilege structure. */
#define PREEMPTIBLE 0x002 /* kernel tasks are not preemptible */
#define BILLABLE 0x004 /* some processes are not billable */
+1 -1
View File
@@ -31,7 +31,7 @@ struct sprof_sample {
struct sprof_proc {
endpoint_t proc;
char name[8];
char name[PROC_NAME_LEN];
};
#include <minix/types.h>
+2 -3
View File
@@ -37,9 +37,8 @@ int sys_abort(int how, ...);
int sys_enable_iop(endpoint_t proc_ep);
int sys_exec(endpoint_t proc_ep, char *ptr, char *aout, vir_bytes
initpc);
int sys_fork(endpoint_t parent, endpoint_t child, endpoint_t *, struct
mem_map *ptr, u32_t vm, vir_bytes *);
int sys_newmap(endpoint_t proc_ep, struct mem_map *ptr);
int sys_fork(endpoint_t parent, endpoint_t child, endpoint_t *,
u32_t vm, vir_bytes *);
int sys_clear(endpoint_t proc_ep);
int sys_exit(void);
int sys_trace(int req, endpoint_t proc_ep, long addr, long *data_p);
+50 -49
View File
@@ -1,5 +1,6 @@
#ifndef _TYPE_H
#define _TYPE_H
#include <machine/multiboot.h>
#ifndef _MINIX_SYS_CONFIG_H
#include <minix/sys_config.h>
@@ -9,6 +10,9 @@
#include <minix/types.h>
#endif
#include <minix/const.h>
#include <minix/com.h>
#include <stdint.h>
/* Type definitions. */
@@ -16,39 +20,12 @@ typedef unsigned int vir_clicks; /* virtual addr/length in clicks */
typedef unsigned long phys_bytes; /* physical addr/length in bytes */
typedef unsigned int phys_clicks; /* physical addr/length in clicks */
typedef int endpoint_t; /* process identifier */
typedef int32_t cp_grant_id_t; /* A grant ID. */
#if (_MINIX_CHIP == _CHIP_INTEL)
typedef long unsigned int vir_bytes; /* virtual addresses/lengths in bytes */
#endif
#if (_MINIX_CHIP == _CHIP_M68000)
typedef unsigned long vir_bytes;/* virtual addresses and lengths in bytes */
#endif
#if (_MINIX_CHIP == _CHIP_SPARC)
typedef unsigned long vir_bytes;/* virtual addresses and lengths in bytes */
#endif
/* Memory map for local text, stack, data segments. */
struct mem_map {
vir_clicks mem_vir; /* virtual address */
phys_clicks mem_phys; /* physical address */
vir_clicks mem_len; /* length */
};
/* Memory map for remote memory areas, e.g., for the RAM disk. */
struct far_mem {
int in_use; /* entry in use, unless zero */
phys_clicks mem_phys; /* physical address */
vir_clicks mem_len; /* length */
};
/* Structure for virtual copying by means of a vector with requests. */
struct vir_addr {
endpoint_t proc_nr_e;
int segment;
endpoint_t proc_nr_e; /* NONE for phys, otherwise process endpoint */
vir_bytes offset;
};
@@ -99,27 +76,6 @@ struct sigmsg {
vir_bytes sm_stkptr; /* user stack pointer */
};
/* This is used to obtain system information through SYS_GETINFO. */
struct kinfo {
phys_bytes code_base; /* base of kernel code */
phys_bytes code_size;
phys_bytes data_base; /* base of kernel data */
phys_bytes data_size;
vir_bytes proc_addr; /* virtual address of process table */
phys_bytes _kmem_base; /* kernel memory layout (/dev/kmem) */
phys_bytes _kmem_size;
phys_bytes bootdev_base; /* boot device from boot image (/dev/boot) */
phys_bytes bootdev_size;
phys_bytes ramdev_base; /* boot device from boot image (/dev/boot) */
phys_bytes ramdev_size;
phys_bytes _params_base; /* parameters passed by boot monitor */
phys_bytes _params_size;
int nr_procs; /* number of user processes */
int nr_tasks; /* number of kernel tasks */
char release[6]; /* kernel release number */
char version[6]; /* kernel version number */
};
/* Load data accounted every this no. of seconds. */
#define _LOAD_UNIT_SECS 6 /* Changing this breaks ABI. */
@@ -166,12 +122,55 @@ struct mem_range
phys_bytes mr_limit; /* Highest memory address in range */
};
/* List of boot-time processes set in kernel/table.c. */
struct boot_image {
int proc_nr; /* process number to use */
char proc_name[PROC_NAME_LEN]; /* name in process table */
endpoint_t endpoint; /* endpoint number when started */
phys_bytes start_addr; /* Where it's in memory */
phys_bytes len;
};
/* Memory chunks. */
struct memory {
phys_bytes base;
phys_bytes size;
};
/* This is used to obtain system information through SYS_GETINFO. */
#define MAXMEMMAP 40
typedef struct kinfo {
/* Straight multiboot-provided info */
multiboot_info_t mbi;
multiboot_module_t module_list[MULTIBOOT_MAX_MODS];
multiboot_memory_map_t memmap[MAXMEMMAP]; /* free mem list */
phys_bytes mem_high_phys;
int mmap_size;
/* Multiboot-derived */
int mods_with_kernel; /* no. of mods incl kernel */
int kern_mod; /* which one is kernel */
/* Minix stuff, started at bootstrap phase */
int freepde_start; /* lowest pde unused kernel pde */
char param_buf[MULTIBOOT_PARAM_BUF_SIZE];
/* Minix stuff */
struct kmessages *kmess;
int do_serial_debug; /* system serial output */
int serial_debug_baud; /* serial baud rate */
int minix_panicing; /* are we panicing? */
vir_bytes user_sp; /* where does kernel want stack set */
vir_bytes user_end; /* upper proc limit */
vir_bytes vir_kern_start; /* kernel addrspace starts */
vir_bytes bootstrap_start, bootstrap_len;
struct boot_image boot_procs[NR_BOOT_PROCS];
int nr_procs; /* number of user processes */
int nr_tasks; /* number of kernel tasks */
char release[6]; /* kernel release number */
char version[6]; /* kernel version number */
} kinfo_t;
#define STATICINIT(v, n) \
if(!(v)) { \
if(!((v) = alloc_contig(sizeof(*(v)) * (n), 0, NULL))) { \
@@ -184,6 +183,8 @@ struct kmessages {
int km_next; /* next index to write */
int km_size; /* current size in buffer */
char km_buf[_KMESS_BUF_SIZE]; /* buffer for messages */
char kmess_buf[80*25]; /* printable copy of message buffer */
int blpos; /* kmess_buf position */
};
#include <minix/config.h>
-1
View File
@@ -55,7 +55,6 @@ struct vm_usage_info {
};
struct vm_region_info {
int vri_seg; /* segment of virtual region (T or D) */
vir_bytes vri_addr; /* base address of region */
vir_bytes vri_length; /* length of region */
int vri_prot; /* protection flags (PROT_) */