Index: trunk/hal/x86_64/hal_boot.S
===================================================================
--- trunk/hal/x86_64/hal_boot.S	(revision 25)
+++ trunk/hal/x86_64/hal_boot.S	(revision 29)
@@ -2,17 +2,15 @@
  * hal_boot.S - Kernel boot entry point
  * 
- * Author        Maxime Villard (2017)
- *               This code is inspired a lot from the NetBSD boot procedure,
- *               written by Maxime Villard too.
- *
- * Copyright (c) UPMC Sorbonne Universites
- * 
- * This file is part of ALMOS-kernel.
- *
- * ALMOS-kernel is free software; you can redistribute it and/or modify it
+ * Copyright (c) 2017 Maxime Villard
+ * This code is inspired a lot from the NetBSD boot procedure, written by
+ * Maxime Villard too. XXX copyright
+ *
+ * This file is part of ALMOS-MKH.
+ *
+ * ALMOS-MKH is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; version 2.0 of the License.
  *
- * ALMOS-kernel is distributed in the hope that it will be useful, but
+ * ALMOS-MKH is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@ -20,8 +18,9 @@
  *
  * You should have received a copy of the GNU General Public License
- * along with ALMOS-kernel; if not, write to the Free Software Foundation,
+ * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define x86_ASM
 #include <hal_boot.h>
 #include <hal_register.h>
@@ -99,4 +98,14 @@
 2:					;
 
+/*
+ * killkpt - Destroy a kernel page table (long mode)
+ *	rbx = page table address
+ *	rcx = number of pages to destroy
+ */
+#define killkpt \
+1:	movq	$0,(%rbx)	; \
+	addq	$PDE_SIZE,%rbx	; \
+	loop	1b		;
+
 /* 32bit version of PG_NX */
 #define PG_NX32	0x80000000
@@ -106,4 +115,5 @@
 	.globl	start_x86_64
 	.globl	L4paddr
+	.globl	iom_base
 
 /*
@@ -125,4 +135,8 @@
 	.type	L4paddr, @object
 	L4paddr:	.quad	0	/* paddr of L4 */
+
+	.type	iom_base, @object
+	iom_base:	.quad	0	/* virt. addr. of ISA I/O MEM */
+
 
 #define GDT64_LIMIT gdt64_end-gdt64_start-1
@@ -152,5 +166,5 @@
 farjmp64:
 	.long	RELOC(longmode)
-	.word	GSEL(GCODE_SEL, SEL_KPL)
+	.word	GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)
 .align 64
 
@@ -169,6 +183,6 @@
 
 	/* Make sure it is a multiboot-compliant bootloader. */
-	cmpl	$MULTIBOOT_INFO_MAGIC,%eax
-	jne	boot_panic
+//	cmpl	$MULTIBOOT_INFO_MAGIC,%eax
+//	jne	boot_panic
 
 	movl	$RELOC(tmpstk),%esp
@@ -246,5 +260,5 @@
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
-	orl	$(PG_V|PG_KR),%eax
+	orl	$(PG_V|PG_KR|PG_G),%eax
 	fillkpt
 
@@ -254,5 +268,5 @@
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
-	orl	$(PG_V|PG_KW),%eax
+	orl	$(PG_V|PG_KW|PG_G),%eax
 	fillkpt_nox
 
@@ -261,5 +275,5 @@
 	movl	$TABLESIZE,%ecx		/* length of BOOTSTRAP TABLES */
 	shrl	$PGSHIFT,%ecx
-	orl	$(PG_V|PG_KW),%eax
+	orl	$(PG_V|PG_KW|PG_G),%eax
 	fillkpt_nox
 
@@ -268,5 +282,5 @@
 	movl	$IOM_SIZE,%ecx	/* size of ISA I/O MEM */
 	shrl	$PGSHIFT,%ecx
-	orl	$(PG_V|PG_KW/*|PG_N*/),%eax
+	orl	$(PG_V|PG_KW|PG_G),%eax
 	fillkpt_nox
 
@@ -335,5 +349,5 @@
 	 */
 	movl	%cr4,%eax
-	orl	$(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT),%eax
+	orl	$(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT|CR4_PGE),%eax
 	movl	%eax,%cr4
 
@@ -381,5 +395,5 @@
 	 */
 	movabsq	$longmode_hi,%rax
-	jmp	*%rax /* XXX XXX: <------ this instruction faults */
+	jmp	*%rax
 
 longmode_hi:
@@ -397,13 +411,41 @@
 	movq	$KERNBASE,%r8
 
-
-	/*
-	 * STOP HERE FOR NOW
-	 */
-1:	nop
-	jmp	1b
-
-
-boot_panic:
-	jmp	boot_panic
-
+#if L2_SLOT_KERNBASE > 0
+	movq	$(NKL2_KIMG_ENTRIES+1),%rcx
+	leaq	(PROC0_PTP2_OFF)(%rsi),%rbx	/* old, phys address */
+	addq	%r8,%rbx			/* new, virt address */
+	killkpt
+#endif
+
+#if L3_SLOT_KERNBASE > 0
+	movq	$NKL3_KIMG_ENTRIES,%rcx
+	leaq	(PROC0_PTP3_OFF)(%rsi),%rbx	/* old, phys address */
+	addq	%r8,%rbx			/* new, virt address */
+	killkpt
+#endif
+
+	movq	$NKL4_KIMG_ENTRIES,%rcx
+	leaq	(PROC0_PML4_OFF)(%rsi),%rbx	/* old, phys address of PML4 */
+	addq	%r8,%rbx			/* new, virt address of PML4 */
+	killkpt
+
+	/* Relocate atdevbase. */
+	movq	$(TABLESIZE+KERNBASE),%rdx
+	addq	%rsi,%rdx
+	movq	%rdx,iom_base(%rip)
+
+	/* Set up bootstrap stack. */
+	leaq	(PROC0_STK_OFF)(%rsi),%rax
+	addq	%r8,%rax
+	leaq	(STKSIZE)(%rax),%rsp
+	xorq	%rbp,%rbp			/* mark end of frames */
+
+	xorw	%ax,%ax
+	movw	%ax,%gs
+	movw	%ax,%fs
+
+	/* The first physical page available. */
+	leaq	(TABLESIZE)(%rsi),%rdi
+
+	call	init_x86_64
+
