Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 gcl (2.6.7+dfsga-23) unstable; urgency=high
 .
   * fix for maxima on kfbsd and sparc
Author: Camm Maguire <camm@debian.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: http://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- gcl-2.6.7+dfsga.orig/configure
+++ gcl-2.6.7+dfsga/configure
@@ -2803,7 +2803,7 @@ case $canonical in
 	use=amd64-linux;;
 
      *x86_64*kfreebsd*)
-	use=amd64-linux;;
+	use=amd64-kfreebsd;;
 
      *86*linux*)
 	use=386-linux;;
@@ -2999,6 +2999,8 @@ def_pic="no";
 def_static="no";
 def_debug="no";
 case $use in
+     *kfreebsd)
+	ln -snf linux.defs h/$use.defs;;
      *gnu)
 	ln -snf linux.defs h/$use.defs;;
      *linux)
--- gcl-2.6.7+dfsga.orig/configure.in
+++ gcl-2.6.7+dfsga/configure.in
@@ -125,7 +125,7 @@ case $canonical in
 	use=amd64-linux;;
 
      *x86_64*kfreebsd*)
-	use=amd64-linux;;
+	use=amd64-kfreebsd;;
 
      *86*linux*)
 	use=386-linux;;
@@ -321,6 +321,8 @@ def_pic="no";
 def_static="no";
 def_debug="no";
 case $use in 
+     *kfreebsd)
+	ln -snf linux.defs h/$use.defs;;
      *gnu)
 	ln -snf linux.defs h/$use.defs;;
      *linux)
--- /dev/null
+++ gcl-2.6.7+dfsga/h/amd64-kfreebsd.h
@@ -0,0 +1,43 @@
+#include "linux.h"
+
+#ifdef IN_GBC
+#undef MPROTECT_ACTION_FLAGS
+#define MPROTECT_ACTION_FLAGS SA_RESTART|SA_SIGINFO
+#define GET_FAULT_ADDR(sig,code,sv,a) \
+ ((siginfo_t *)code)->si_addr
+#endif
+
+/*#define NULL_OR_ON_C_STACK(x) ((x)==0 || ((unsigned int)x) > (unsigned int)(pagetochar(MAXPAGE+1)))*/
+
+#define ADDITIONAL_FEATURES \
+		     ADD_FEATURE("BSD386"); \
+      	             ADD_FEATURE("MC68020")
+
+
+#define	I386
+#define SGC
+
+/* Apparently stack pointers can be 4 byte aligned, at least &argc -- CM */
+#define C_GC_OFFSET 4
+
+#ifdef IN_SFASL
+#include <sys/mman.h>
+#define CLEAR_CACHE {\
+   void *p,*pe; \
+   p=(void *)((unsigned long)memory->cfd.cfd_start & ~(PAGESIZE-1)); \
+   pe=(void *)((unsigned long)(memory->cfd.cfd_start+memory->cfd.cfd_size) & ~(PAGESIZE-1)) + PAGESIZE-1; \
+   if (mprotect(p,pe-p,PROT_READ|PROT_WRITE|PROT_EXEC)) {\
+     fprintf(stderr,"%p %p\n",p,pe);\
+     perror("");\
+     FEerror("Cannot mprotect", 0);\
+   }\
+}
+#endif
+
+#define RELOC_H "elf64_i386_reloc.h"
+
+
+#ifndef HAVE_SYSCONF_PHYS_PAGES
+#error need SYSCONF_PHYS_PAGES to set real_maxpage
+#endif
+#define BRK_DOES_NOT_GUARANTEE_ALLOCATION
--- gcl-2.6.7+dfsga.orig/h/386-kfreebsd.h
+++ gcl-2.6.7+dfsga/h/386-kfreebsd.h
@@ -59,3 +59,8 @@
 #endif
 
 #define RELOC_H "elf32_i386_reloc.h"
+
+#ifndef HAVE_SYSCONF_PHYS_PAGES
+#error need SYSCONF_PHYS_PAGES to set real_maxpage
+#endif
+#define BRK_DOES_NOT_GUARANTEE_ALLOCATION
--- gcl-2.6.7+dfsga.orig/o/main.c
+++ gcl-2.6.7+dfsga/o/main.c
@@ -165,6 +165,9 @@ update_real_maxpage(void) {
 
 #ifdef HAVE_SYSCONF_PHYS_PAGES
   phys_pages=sysconf(_SC_PHYS_PAGES);
+#ifdef BRK_DOES_NOT_GUARANTEE_ALLOCATION
+  if (real_maxpage>phys_pages) for (real_maxpage=1;real_maxpage<phys_pages;real_maxpage<<=1);
+#endif
 #endif
 
   available_pages=real_maxpage-first_data_page-resv_pages;
--- gcl-2.6.7+dfsga.orig/o/sgbc.c
+++ gcl-2.6.7+dfsga/o/sgbc.c
@@ -1438,8 +1438,8 @@ sgc_start(void) {
 	object x=v;
 	if (type_of(x)!=t_stream || is_free(x)) continue;
 	if (x->sm.sm_buffer) 
-	  for (i=0;i<(BUFSIZ+PAGESIZE-1)/PAGESIZE;i++)
-	    SET_WRITABLE(page(x->sm.sm_buffer)+i);
+	  for (i=page(x->sm.sm_buffer);i<=page(x->sm.sm_buffer+BUFSIZ-1);i++)
+	    SET_WRITABLE(i);
       }
 #endif
     }
