It appears that in CMUCL it possible to handle SIGSEGV conditions.



(defun mmap-anonymous-region (size prot)
  (unix::unix-mmap nil size prot (logior UNIX:MAP_ANONYMOUS  unix::MAP_PRIVATE) nil 0))

(defparameter *mmap-sap* (mmap-anonymous-region (* 4 (SYS:GET-PAGE-SIZE )) unix::PROT_NONE))

(handler-case (progn (unix:unix-mprotect *mmap-sap* (* 4 (sys:get-page-size )) unix::prot_read)
             (setf (sys::sap-ref-32 *mmap-sap* 0) 1))
  (error ()
    (unix:unix-mprotect *mmap-sap* (* 4 (sys:get-page-size )) unix::prot_write)
    (setf (sys::sap-ref-32 *mmap-sap* 0) 1)
    ))

 
Has SBCL diverged from CMUCL in this regard? 



Gábor Melis wrote:
On Martes 14 Abril 2009, Lynn Quam wrote:
  
I have been doing some experiments using mmap with
sbcl-1.0.25-x86-64-linux. I have been able to sucessfully mmap to
files and access the mmapped pages from lisp.

Another thing I would like to do is create mmapped pages using
MAP_ANONYMOUS which are not mapped to files, and the pages initially
allow neither read or write access.  I would like
to handle SIGSEGV interrupts to those pages to invoke code to compute
the contents of the pages, and then resume the interupted code.

An example use of such a capability is to access large jpeg
compressed tiled TIFF images.
A large region of virtual memory is mmapped using MAP_ANONYMOUS for
the uncompressed image tiles (whose size are multiples of the mmap
page size).  When a SIGSEGV occurs, the appropriate tile is
decompressed from the TIFF image into the uncompressed mmapped tile,
and the PROT_READ is set on those memory pages.

The problem I am having is that SBCL calls SB-SYS:MEMORY-FAULT-ERROR
which has no prevision
for invoking a user supplied interrupt-handler or for resuming the
code that triggered the SIGSEGV.  Is there any hope not such a
capability?
    

SIGSEGV is handled on the altstack. The handler frobs the interrupt 
context on the normal stack so that a lisp function that signals memory 
fault is invoked when the handler returns. On the altstack no lisp code 
can be run so you are out of luck trying to do this via the lisp 
condition system.

One could imagine a lisp api for registering handlers for regions of 
memory which would allow the runtime to invoke a user defined handler 
with the fault address.

Cheers,
Gabor