Skip to contentSkip to conversation

Integrating applications into Mini-OS

PATCH In review John D. Ramsdell 3 participants 10 messages May 11, 2006 – May 15, 2006 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Integrating applications into Mini-OS

I sent this patch directly to Gregor, and neglected to CC this list. Sorry about that.

I'd like to make it so that there is no need to modify any Mini-OS source files when extending it with an application. All that is required is a change to Makefile, and a small change to kernel.c, printf.c, and string.c. I have enclosed the patch.

With this patch, one could write an application in a file called, say, app.c, and add it to the Mini-OS directory along with newlib.c and setjmp_x86_32.S, the two files I use to adapt newlib to the Mini-OS environment. One would compile and link it with newlib, with the command:

#! /bin/sh
COMP_HOME=${HOME}/opt/cross/i386-elf CPPFLAGS="-DINIT_APP -DHAVE_LIBC"
LDLIBS="-lc -lnosys"
export PATH=${COMP_HOME}/bin:$PATH
exec make CPPFLAGS="${CPPFLAGS}" LDLIBS="${LDLIBS}" "$@"

The patch changes Makefile so that it needs no modification to handle an application. It also fixes some problems with the original Makefile. The original defined LDFLAGS, but did not use the definition in the linker command. There was some odd whitespace after the last update to OBJS, which has been removed. I also changed the rules for compiling C and assembler files, so they more closely match the default rules built into GNU Make.

John

Only in xen-unstable/extras/mini-os: app.c
Only in xen-unstable/extras/mini-os: app.h
Only in xen-unstable/extras/mini-os: app.lua
Only in xen-unstable/extras/mini-os: bin2c
Only in xen-unstable/extras/mini-os: bin2c.mk
Only in xen-unstable/extras/mini-os: crmake

extras/mini-os/kernel.c +8 -0
diff -ur oxen-unstable/extras/mini-os/kernel.c xen-unstable/extras/mini-os/kernel.c
@@ -38,6 +38,10 @@
 #include <xen/features.h>
 #include <xen/version.h>
 
+#if defined INIT_APP
+    void init_app(void);
+#endif
+
 /*
  * Shared page for communicating with the hypervisor.
  * Events flags go here, for example.
@@ -171,6 +175,10 @@
     /* Init XenBus from a separate thread */
     create_thread("init_xs", init_xs, NULL);
 
+#if defined INIT_APP
+    init_app();
+#endif
+
     /* Everything initialised, start idle thread */
     run_idle_thread();
 }
extras/mini-os/lib/printf.c +3 -1
diff -ur oxen-unstable/extras/mini-os/lib/printf.c xen-unstable/extras/mini-os/lib/printf.c
@@ -54,6 +54,8 @@
  * $FreeBSD: src/sys/libkern/divdi3.c,v 1.6 1999/08/28 00:46:31 peter Exp $
  */
 
+#if !defined HAVE_LIBC
+
 #include <os.h>
 #include <types.h>
 #include <hypervisor.h>
@@ -789,4 +791,4 @@
 	return i;
 }
 
-
+#endif

Only in xen-unstable/extras/mini-os/lib: printf.c~

extras/mini-os/lib/string.c +4 -0
diff -ur oxen-unstable/extras/mini-os/lib/string.c xen-unstable/extras/mini-os/lib/string.c
@@ -18,6 +18,8 @@
  ****************************************************************************
  */
 
+#if !defined HAVE_LIBC
+
 #include <os.h>
 #include <types.h>
 #include <lib.h>
@@ -153,3 +155,5 @@
         }
         return NULL;
 }
+
+#endif

Only in xen-unstable/extras/mini-os/lib: string.c~

extras/mini-os/Makefile +13 -8
diff -ur oxen-unstable/extras/mini-os/Makefile xen-unstable/extras/mini-os/Makefile
@@ -6,18 +6,23 @@
 override TARGET_ARCH     := $(XEN_TARGET_ARCH)
 
 # NB. '-Wcast-qual' is nasty, so I omitted it.
-CFLAGS := -fno-builtin -Wall -Werror -Iinclude/ -Wredundant-decls -Wno-format
+CFLAGS := -fno-builtin -Wall -Werror -Wredundant-decls -Wno-format
 CFLAGS += -Wstrict-prototypes -Wnested-externs -Wpointer-arith -Winline
 
+override CPPFLAGS := -Iinclude $(CPPFLAGS)
+ASFLAGS = -D__ASSEMBLY__
+
+LDFLAGS := -N -T minios-$(TARGET_ARCH).lds
+
 ifeq ($(TARGET_ARCH),x86_32)
 CFLAGS += -m32 -march=i686
-LDFLAGS := -m elf_i386
+LDFLAGS += -m elf_i386
 endif
 
 ifeq ($(TARGET_ARCH),x86_64)
 CFLAGS += -m64 -mno-red-zone -fpic -fno-reorder-blocks
 CFLAGS += -fno-asynchronous-unwind-tables
-LDFLAGS := -m elf_x86_64
+LDFLAGS += -m elf_x86_64
 endif
 
 ifeq ($(debug),y)
@@ -28,12 +33,12 @@
 
 TARGET := mini-os
 
-OBJS := $(TARGET_ARCH).o
+OBJS := $(patsubst %.S,%.o,$(wildcard *$(TARGET_ARCH).S))
 OBJS += $(patsubst %.c,%.o,$(wildcard *.c))
 OBJS += $(patsubst %.c,%.o,$(wildcard lib/*.c))
 OBJS += $(patsubst %.c,%.o,$(wildcard xenbus/*.c))
 OBJS += $(patsubst %.c,%.o,$(wildcard console/*.c))
-										   
+
 HDRS := $(wildcard include/*.h)
 HDRS += $(wildcard include/xen/*.h)
 
@@ -45,7 +50,7 @@
 	[ -e include/xen ] || ln -sf ../../../xen/include/public include/xen
 
 $(TARGET): links $(OBJS)
-	$(LD) -N -T minios-$(TARGET_ARCH).lds $(OBJS) -o [email protected]
+	$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o [email protected]
 	gzip -f -9 -c [email protected] >[email protected]
 
 .PHONY: clean
@@ -55,10 +60,10 @@
 	find . -type l | xargs rm -f
 
 %.o: %.c $(HDRS) Makefile
-	$(CC) $(CFLAGS) -c $< -o $@
+	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
 
 %.o: %.S $(HDRS) Makefile
-	$(CC) $(CFLAGS) -D__ASSEMBLY__ -c $< -o $@
+	$(CC) $(ASFLAGS) $(CPPFLAGS) -c $< -o $@
 
 define all_sources
      ( find . -follow -name SCCS -prune -o -name '*.[chS]' -print )

Only in xen-unstable/extras/mini-os: newlib.c Only in xen-unstable/extras/mini-os: setjmp_x86_32.S

#1 lore raw
Quoted 10 lines · John D. Ramsdell

I'd like to make it so that there is no need to modify any Mini-OS source files when extending it with an application. All that is required is a change to Makefile, and a small change to kernel.c, printf.c, and string.c. I have enclosed the patch.

With this patch, one could write an application in a file called, say, app.c, and add it to the Mini-OS directory along with newlib.c and setjmp_x86_32.S, the two files I use to adapt newlib to the Mini-OS environment. One would compile and link it with newlib, with the command:

hi,

I think it would be more elegant to link the mini-os C files as a 'libminios.a' lib, and then you can replace main() with your own by linking libminios.a from a separate Makefile somewhere else in you tree.

I have attached a small patch to mini-os which changes the mini-os Makefile and adds a dummy main function which you can override by linking with your own main() first, as in:

OBJS := mymain.o

myapp: $(OBJS)
$(LD) -N -T myapp.lds ../mini-os/x86_32.o $(OBJS) -o myapp.elf
-lminios -L../mini-os

regards,
Jacob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw
Quoted 1 line · Jacob Gorm Hansen

I have attached a small patch to mini-os which ... adds a dummy main

Should it be marked with __attribute__((weak))?

http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Function-Attributes.html#Function-Attributes
...
weak
The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.

Ian.

#3 lore raw
Quoted 4 lines · Ian Campbell
Quoted 1 line · Jacob Gorm Hansen

I have attached a small patch to mini-os which ... adds a dummy main

Should it be marked with __attribute__((weak))?

Probably a good idea, makes link order less important. Revised patch is attached.

Jacob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw
Quoted 8 lines · Jacob Gorm Hansen
Quoted 4 lines · Ian Campbell
Quoted 1 line · Jacob Gorm Hansen

I have attached a small patch to mini-os which ... adds a dummy main

Should it be marked with __attribute__((weak))?

Probably a good idea, makes link order less important. Revised patch is attached.

One more change: When providing one's own linker script (e.g. for changing the memory layout, my boot loader cannot exist at 0xc0000000 but likes to live at 0x0), use &_text for the VIRT_START constant instead of hardcoded value.

New version attached.

Jacob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#10 lore raw
In reply to Jacob Gorm Hansen
Quoted 3 lines · Jacob Gorm Hansen

I have attached a small patch to mini-os which changes the mini-os Makefile and adds a dummy main function which you can override by linking with your own main() first, as in:

The primary goal of my patch is to change the Mini-OS sources so that one need not modify source files in the Mini-OS distribution to add an application. I'm not wedded to the particulars of my patch, however, your patch is insufficient for my goals. You see, sometimes one wants to build Mini-OS using the newlib C library, and sometimes one wants to build Mini-OS so it uses only the subset of the C library that comes with Mini-OS. I really need #if !defined HAVE_LIBC preprocessor commands to be added to lib/printf.c and lib/string.c.

John

#6 lore raw
In reply to a message not in the archive
Quoted 7 lines · John D. Ramsdell

I didn't want to say this on the list, but another feature about the patch I submitted is I cleaned up various problems with the Makefile. It adds CPPFLAGS and ASFLAGS, for example, and its use of LDFLAGS is broken. Please apply my patch to the base Makefile, and then make your changes. Submit the combined patch to the list. Let's use this opportunity to fix many problems at once. Gregor will appreciate a well thought out change.

New version incorporating most of your changes to the Makefile attached.

Jacob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#7 lore raw
Quoted 2 lines · Jacob Gorm Hansen

New version incorporating most of your changes to the Makefile attached.

Thank you very much. The change is very important to me because I depend on an overridable CPPFLAGS, so that I can define -DHAVE_LIBC when compiling lib/printf.c and lib/string.c. This is how I slip in the full blown C library newlib.

John

#8 lore raw
In reply to Jacob Gorm Hansen
Quoted 2 lines · Jacob Gorm Hansen

New version incorporating most of your changes to the Makefile attached.

Opps. I just noticed you took one line too much of my Makefile patch. This part

-OBJS := $(TARGET_ARCH).o
-OBJS += $(patsubst %.c,%.o,$(wildcard *.c)) +HEAD := $(patsubst %.S,%.o,$(wildcard *$(TARGET_ARCH).S)) +OBJS := $(patsubst %.c,%.o,$(wildcard *.c))

should be replaced with

-OBJS := $(TARGET_ARCH).o
-OBJS += $(patsubst %.c,%.o,$(wildcard *.c))
+HEAD := $(TARGET_ARCH).o
+OBJS := $(patsubst %.c,%.o,$(wildcard *.c))

as with your scheme, I no longer have to add my assembly routine that provides setjmp and longjmp into the Mini-OS source tree.

John

#9 lore raw
Quoted 7 lines · John D. Ramsdell
Quoted 2 lines · Jacob Gorm Hansen

New version incorporating most of your changes to the Makefile attached.

Opps. I just noticed you took one line too much of my Makefile patch. This part

Fine, new version of the patch attached.

Jacob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel