Index: generic/include/mm/buddy.h
===================================================================
--- generic/include/mm/buddy.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/buddy.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2005 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __BUDDY_H__
+#define __BUDDY_H__
+
+#include <arch/types.h>
+#include <typedefs.h>
+
+#define BUDDY_SYSTEM_INNER_BLOCK	0xff
+
+struct buddy_system_operations {
+	link_t *(* find_buddy)(link_t *);
+	link_t *(* bisect)(link_t *);
+	link_t *(* coalesce)(link_t *, link_t *);
+	void (*set_order)(link_t *, __u8);
+	__u8 (*get_order)(link_t *);
+};
+
+struct buddy_system {
+	__u8 max_order;
+	link_t *order;
+	buddy_system_operations_t *op;
+};
+
+extern buddy_system_t *buddy_system_create(__u8 max_order, buddy_system_operations_t *op);
+extern link_t *buddy_system_alloc(buddy_system_t *b, __u8 i);
+extern void buddy_system_free(buddy_system_t *b, link_t *block);
+
+#endif
Index: generic/include/mm/frame.h
===================================================================
--- generic/include/mm/frame.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/frame.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2005 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __FRAME_H__
+#define __FRAME_H__
+
+#include <arch/types.h>
+#include <typedefs.h>
+#include <list.h>
+#include <synch/spinlock.h>
+
+#define FRAME_KA	1	/* skip frames conflicting with user address space */
+#define FRAME_PANIC	2	/* panic on failure */
+
+struct zone {
+	link_t link;		/**< link to previous and next zone */
+
+	spinlock_t lock;	/**< this lock protects everything below */
+	__address base;		/**< physical address of the first frame in the frames array */
+	frame_t *frames;	/**< array of frame_t structures in this zone */
+	link_t free_head;	/**< list of free frame_t structures */
+	count_t free_count;	/**< number of frame_t structures in free list */
+	count_t busy_count;	/**< number of frame_t structures not in free list */
+	int flags;
+};
+
+struct frame {
+	count_t refcount;	/**< when == 0, the frame is in free list */
+	link_t link;		/**< link to zone free list when refcount == 0 */
+};
+
+extern spinlock_t zone_head_lock;	/**< this lock protects zone_head list */
+extern link_t zone_head;		/**< list of all zones in the system */
+
+extern void zone_init(void);
+extern zone_t *zone_create(__address start, size_t size, int flags);
+extern void zone_attach(zone_t *zone);
+
+extern void frame_init(void);
+extern void frame_initialize(frame_t *frame, zone_t *zone);
+__address frame_alloc(int flags);
+extern void frame_free(__address addr);
+extern void frame_not_free(__address addr);
+extern void frame_region_not_free(__address start, __address stop);
+
+/*
+ * TODO: Implement the following functions.
+ */
+extern frame_t *frame_reference(frame_t *frame);
+extern void frame_release(frame_t *frame);
+
+#endif
Index: generic/include/mm/heap.h
===================================================================
--- generic/include/mm/heap.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/heap.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2001-2004 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __HEAP_H__
+#define __HEAP_H__
+
+#include <arch/types.h>
+#include <typedefs.h>
+
+#define malloc(size)		early_malloc(size)
+#define free(ptr)		early_free(ptr)
+
+struct chunk {
+	int used;
+	chunk_t *next;
+	chunk_t *prev;
+	__u32 size;
+	__native data[0];
+};
+
+extern void early_heap_init(__address heap, size_t size);
+
+extern void *early_malloc(size_t size) __attribute__ ((malloc));
+extern void early_free(void *ptr);
+
+#endif
Index: generic/include/mm/page.h
===================================================================
--- generic/include/mm/page.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/page.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2001-2004 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __PAGE_H__
+#define __PAGE_H__
+
+#include <arch/types.h>
+#include <typedefs.h>
+
+#define PAGE_CACHEABLE_SHIFT		0
+#define PAGE_NOT_CACHEABLE_SHIFT	PAGE_CACHEABLE_SHIFT
+#define PAGE_PRESENT_SHIFT		1
+#define PAGE_NOT_PRESENT_SHIFT		PAGE_PRESENT_SHIFT
+#define PAGE_USER_SHIFT			2
+#define PAGE_KERNEL_SHIFT		PAGE_USER_SHIFT
+#define PAGE_READ_SHIFT			3
+#define PAGE_WRITE_SHIFT		4
+#define PAGE_EXEC_SHIFT			5
+
+#define PAGE_NOT_CACHEABLE	(0<<PAGE_CACHEABLE_SHIFT)
+#define PAGE_CACHEABLE		(1<<PAGE_CACHEABLE_SHIFT)
+
+#define PAGE_PRESENT		(0<<PAGE_PRESENT_SHIFT)
+#define PAGE_NOT_PRESENT	(1<<PAGE_PRESENT_SHIFT)
+
+#define PAGE_USER		(1<<PAGE_USER_SHIFT)
+#define PAGE_KERNEL		(0<<PAGE_USER_SHIFT)
+
+#define PAGE_READ		(1<<PAGE_READ_SHIFT)
+#define PAGE_WRITE		(1<<PAGE_WRITE_SHIFT)
+#define PAGE_EXEC		(1<<PAGE_EXEC_SHIFT)
+
+/*
+ * This is the generic 4-level page table interface.
+ * Architectures are supposed to implement *_ARCH macros.
+ */
+
+/*
+ * These macros process vaddr and extract those portions
+ * of it that function as indices to respective page tables.
+ */
+#define PTL0_INDEX(vaddr)		PTL0_INDEX_ARCH(vaddr)
+#define PTL1_INDEX(vaddr)		PTL1_INDEX_ARCH(vaddr)
+#define PTL2_INDEX(vaddr)		PTL2_INDEX_ARCH(vaddr)
+#define PTL3_INDEX(vaddr)		PTL3_INDEX_ARCH(vaddr)
+
+#define GET_PTL0_ADDRESS()		GET_PTL0_ADDRESS_ARCH()
+#define SET_PTL0_ADDRESS(ptl0)		SET_PTL0_ADDRESS_ARCH(ptl0)
+
+/*
+ * These macros traverse the 4-level tree of page tables,
+ * each descending by one level.
+ */
+#define GET_PTL1_ADDRESS(ptl0, i)	GET_PTL1_ADDRESS_ARCH(ptl0, i)
+#define GET_PTL2_ADDRESS(ptl1, i)	GET_PTL2_ADDRESS_ARCH(ptl1, i)
+#define GET_PTL3_ADDRESS(ptl2, i)	GET_PTL3_ADDRESS_ARCH(ptl2, i)
+#define GET_FRAME_ADDRESS(ptl3, i)	GET_FRAME_ADDRESS_ARCH(ptl3, i)
+
+/*
+ * These macros are provided to change shape of the 4-level
+ * tree of page tables on respective level.
+ */
+#define SET_PTL1_ADDRESS(ptl0, i, a)	SET_PTL1_ADDRESS_ARCH(ptl0, i, a)
+#define SET_PTL2_ADDRESS(ptl1, i, a)	SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
+#define SET_PTL3_ADDRESS(ptl2, i, a)	SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
+#define SET_FRAME_ADDRESS(ptl3, i, a)	SET_FRAME_ADDRESS_ARCH(ptl3, i, a)
+
+/*
+ * These macros are provided to query various flags within the page tables.
+ */
+#define GET_PTL1_FLAGS(ptl0, i)		GET_PTL1_FLAGS_ARCH(ptl0, i)
+#define GET_PTL2_FLAGS(ptl1, i)		GET_PTL2_FLAGS_ARCH(ptl1, i)
+#define GET_PTL3_FLAGS(ptl2, i)		GET_PTL3_FLAGS_ARCH(ptl2, i)
+#define GET_FRAME_FLAGS(ptl3, i)	GET_FRAME_FLAGS_ARCH(ptl3, i)
+
+/*
+ * These macros are provided to set/clear various flags within the page tables.
+ */
+#define SET_PTL1_FLAGS(ptl0, i, x)	SET_PTL1_FLAGS_ARCH(ptl0, i, x)
+#define SET_PTL2_FLAGS(ptl1, i, x)	SET_PTL2_FLAGS_ARCH(ptl1, i, x)
+#define SET_PTL3_FLAGS(ptl2, i, x)	SET_PTL3_FLAGS_ARCH(ptl2, i, x)
+#define SET_FRAME_FLAGS(ptl3, i, x)	SET_FRAME_FLAGS_ARCH(ptl3, i, x)
+
+extern void page_init(void);
+extern void map_page_to_frame(__address page, __address frame, int flags, __address root);
+extern pte_t *find_mapping(__address page, __address root);
+extern void map_structure(__address s, size_t size);
+
+#endif
Index: generic/include/mm/tlb.h
===================================================================
--- generic/include/mm/tlb.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/tlb.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2001-2004 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __TLB_H__
+#define __TLB_H__
+
+#include <arch/mm/asid.h>
+
+extern void tlb_init(void);
+
+#ifdef __SMP__
+extern void tlb_shootdown_start(void);
+extern void tlb_shootdown_finalize(void);
+extern void tlb_shootdown_ipi_recv(void);
+#else
+#  define tlb_shootdown_start()	;
+#  define tlb_shootdown_finalize()	;
+#  define tlb_shootdown_ipi_recv() ;
+#endif /* __SMP__ */
+
+/* Export TLB interface that each architecture must implement. */
+extern void tlb_init_arch(void);
+extern void tlb_invalidate(asid_t asid);
+extern void tlb_shootdown_ipi_send(void);
+
+#endif
Index: generic/include/mm/vm.h
===================================================================
--- generic/include/mm/vm.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
+++ generic/include/mm/vm.h	(revision 169c408f8774661800aa0a9685c9914cbb5a974c)
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2001-2004 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __VM_H__
+#define __VM_H__
+
+#include <arch/mm/page.h>
+#include <arch/mm/vm.h>
+#include <arch/mm/asid.h>
+#include <arch/types.h>
+#include <typedefs.h>
+#include <synch/spinlock.h>
+#include <list.h>
+
+#define KERNEL_ADDRESS_SPACE_START	KERNEL_ADDRESS_SPACE_START_ARCH
+#define KERNEL_ADDRESS_SPACE_END	KERNEL_ADDRESS_SPACE_END_ARCH
+#define USER_ADDRESS_SPACE_START	USER_ADDRESS_SPACE_START_ARCH
+#define USER_ADDRESS_SPACE_END		USER_ADDRESS_SPACE_END_ARCH
+
+#define IS_KA(addr)	((addr)>=KERNEL_ADDRESS_SPACE_START && (addr)<=KERNEL_ADDRESS_SPACE_END)
+
+#define UTEXT_ADDRESS	UTEXT_ADDRESS_ARCH
+#define USTACK_ADDRESS	USTACK_ADDRESS_ARCH
+#define UDATA_ADDRESS	UDATA_ADDRESS_ARCH
+
+enum vm_type {
+	VMA_TEXT = 1, VMA_DATA, VMA_STACK 
+};
+
+/*
+ * Each vm_area_t structure describes one continuous area of virtual memory.
+ * In the future, it should not be difficult to support shared areas of vm.
+ */
+struct vm_area {
+	spinlock_t lock;
+	link_t link;
+	vm_type_t type;
+	int size;
+	__address address;
+	__address *mapping;
+};
+
+/*
+ * vm_t contains the list of vm_areas of userspace accessible
+ * pages for one or more tasks. Ranges of kernel memory pages are not
+ * supposed to figure in the list as they are shared by all tasks and
+ * set up during system initialization.
+ */
+struct vm {
+	spinlock_t lock;
+	link_t vm_area_head;
+	pte_t *ptl0;
+	asid_t asid;
+};
+
+extern vm_t * vm_create(pte_t *ptl0);
+extern void vm_destroy(vm_t *m);
+
+extern vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr);
+extern void vm_area_destroy(vm_area_t *a);
+
+extern void vm_area_map(vm_area_t *a, vm_t *m);
+extern void vm_area_unmap(vm_area_t *a, vm_t *m);
+
+extern void vm_install(vm_t *m);
+extern void vm_uninstall(vm_t *m);
+
+#endif
