// What a butt-ugly piece of code
#include "syscall_shiny7.h"

// Syscall numbers mipsel SIM_ABI32
#define __NR_Linux			4000
#define __NR_syscall			(__NR_Linux +   0)
#define __NR_exit			(__NR_Linux +   1)
#define __NR_write			(__NR_Linux +   4)
#define __NR_open			(__NR_Linux +   5)
#define __NR_mount			(__NR_Linux +  21)
#define __NR_mmap2			(__NR_Linux + 210)

// mmap2 constants
#define PROT_READ	0x1		/* Page can be read.  */
#define PROT_WRITE	0x2		/* Page can be written.  */
#define MAP_SHARED	0x01		/* Share changes.  */
#define MAP_ANONYMOUS	0x800		/* Don't use a file.  */

// errno, needed by the _syscall* macros
static int errno;

typedef unsigned long int size_t;

_syscall1(void, exit, int, exitcode);
_syscall3(size_t, write, unsigned int, fd, const char *, buf, size_t, count);
_syscall3(long, open, const char *, filename, int, flags, int, mode);
_syscall5(long, mount, char*, dev_name, char*, dir_name, char*, type, unsigned
	  long, flags, void*, data);
_syscall6(void*, mmap2, unsigned long, addr, unsigned long, len, unsigned long,
	  prot, unsigned long, flags, unsigned long, fd, unsigned long, pgoff);

#define write_(fd, str) write(fd, str, sizeof(str))

int main() {
	long int fd;
	char* ptr;
	write_(1, "Starting..\n");

	mount("proc", "/tmp/proc", "proc", 0, (void*)0);
	fd = open("a.c", 0, 0);
	if (fd == -1) {
		write_(2, "Opening a.c failed\n");
		exit(1);
	}

	ptr = mmap2(0, 8*1024, PROT_READ, MAP_SHARED, fd, 1);
	if (ptr == (void*)-1) {
		write_(2, "MMapping a.c failed\n");
		exit(1);
	}

	write_(1, "A piece of mmapped stuff:\n");
	write(1, ptr, 20);
	write_(1, "\nCool, everything finished fine.\n");
	exit(0);
}

/* Some dummy data, so that the mmapped area contains something useful
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 *********************************************************************
 ***********************
 Byte 4097 is at "B" of that "Byte" at the beginning.
 */

