[Git][cmucl/cmucl][issue-97-define-ud2-inst] First cut at using UD1 instead of UD2

Raymond Toy gitlab at common-lisp.net
Fri Apr 9 20:45:54 UTC 2021



Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl


Commits:
93d7cf02 by Raymond Toy at 2021-04-09T13:45:34-07:00
First cut at using UD1 instead of UD2

Disassembly mostly works, except that for the pending interrupt trap,
we print "ud1 ecx, cl" instead of "ud1 ecx, ecx" which is what gdb
prints.

More work still needed.

- - - - -


4 changed files:

- src/compiler/x86/insts.lisp
- src/compiler/x86/macros.lisp
- src/compiler/x86/system.lisp
- src/lisp/x86-assem.S


Changes:

=====================================
src/compiler/x86/insts.lisp
=====================================
@@ -2062,12 +2062,13 @@
  (code :field (byte 8 8)))
 
 
-(disassem:define-instruction-format (ud1 24 :default-printer '(:name :tab code))
-  (op :fields (list (byte 8 0) (byte 8 8)) :value '(#xb00001111 #b10111001))
-  (code :field (byte 8 16)))
-
-(define-emitter emit-ud1-inst 24
-  (byte 8 0) (byte 8 8) (byte 8 16))
+(disassem:define-instruction-format
+    (ud1 24 :default-printer '(:name :tab reg ", " reg/mem))
+  (prefix    :field (byte 8 0) :value #b00001111)
+  (op        :field (byte 8 8) :value #b10111001)
+  (reg/mem   :fields (list (byte 2 22) (byte 3 16))
+	     :type 'reg/mem)
+  (reg	     :field (byte 3 19) :type 'word-reg))
 
 (defun snarf-error-junk (sap offset &optional length-only)
   (let* ((length (system:sap-ref-8 sap offset))
@@ -2099,34 +2100,41 @@
                        (sc-offsets)
                        (lengths))))))))
 
-(defun break-control (chunk inst stream dstate)
+(defun ud1-control (chunk inst stream dstate)
   (declare (ignore inst))
   (flet ((nt (x) (if stream (disassem:note x dstate))))
-    (case (ud1-code chunk dstate)
+    (case (ldb (byte 6 16) chunk)
       (#.vm:error-trap
-       (nt "Error trap")
+       (nt #.(format nil "Error trap: ~D" vm:error-trap))
        (disassem:handle-break-args #'snarf-error-junk stream dstate))
       (#.vm:cerror-trap
-       (nt "Cerror trap")
+       (nt #.(format nil "Cerror trap: ~D" vm:cerror-trap))
        (disassem:handle-break-args #'snarf-error-junk stream dstate))
       (#.vm:breakpoint-trap
-       (nt "Breakpoint trap"))
+       (nt #.(format nil "Breakpoint trap: ~D" vm:breakpoint-trap)))
       (#.vm:pending-interrupt-trap
-       (nt "Pending interrupt trap"))
+       (nt #.(format nil "Pending interrupt trap: ~D" vm:pending-interrupt-trap)))
       (#.vm:halt-trap
-       (nt "Halt trap"))
+       (nt #.(format nil "Halt trap: ~D" vm:halt-trap)))
       (#.vm:function-end-breakpoint-trap
-       (nt "Function end breakpoint trap"))
+       (nt #.(format nil "Function end breakpoint trap: ~D" vm:function-end-breakpoint-trap)))
     )))
 
-;; This is really the int3 instruction.
-(define-instruction break (segment code)
+;; The ud1 instruction where we smash the code (trap type) into the
+;; mod r/m byte.  We don't care about what that actually encodes to.
+;; We just want the trap code in the third byte of the instruction.
+(define-instruction ud1 (segment code)
   (:declare (type (unsigned-byte 8) code))
-  (:printer ud1 ((op '(#b00001111 #b10111001)))
-	    '(:name :tab code)
-	    :control #'break-control)
+  (:printer ud1 ((op #b10111001))
+	    :default
+	    :control #'ud1-control)
   (:emitter
-   (emit-ud1-inst segment #b00001111 #b10111001 code)))
+   (emit-byte segment #x0f)
+   (emit-byte segment #xb9)
+   (emit-mod-reg-r/m-byte segment
+			  #b11
+			  (ldb (byte 3 3) code)
+			  (ldb (byte 3 0) code))))
 
 #+nil
 (define-instruction ud2 (segment)


=====================================
src/compiler/x86/macros.lisp
=====================================
@@ -246,7 +246,7 @@
       `((let ((vop ,vop))
   	  (when vop
 	    (note-this-location vop :internal-error)))
-	(inst break ,kind)		; eg trap_Xyyy
+	(inst ud1 ,kind)		; eg trap_Xyyy
 	(let ((,vector (make-array 8 :element-type '(unsigned-byte 8)
 				   :fill-pointer 0 :adjustable t)))
 	  (write-var-integer (error-number-or-lose ',code) ,vector)
@@ -340,7 +340,7 @@
 				    (- other-pointer-type)))
 	      0)
 	(inst jmp :eq ,label)
-	(inst break pending-interrupt-trap)
+	(inst ud1 pending-interrupt-trap)
 	(emit-label ,label)))))
 
 


=====================================
src/compiler/x86/system.lisp
=====================================
@@ -284,11 +284,11 @@
   (:policy :fast-safe)
   (:translate unix::do-pending-interrupt)
   (:generator 1
-    (inst break pending-interrupt-trap)))
+    (inst ud1 pending-interrupt-trap)))
 
 (define-vop (halt)
   (:generator 1
-    (inst break halt-trap)))
+    (inst ud1 halt-trap)))
 
 (defknown float-wait () (values))
 (define-vop (float-wait)


=====================================
src/lisp/x86-assem.S
=====================================
@@ -18,6 +18,11 @@
 #include "internals.h"
 #include "lispregs.h"
 
+#define TRAP_CODE(code) \
+	.byte 0x0f		; \
+	.byte 0xb9		; \
+	.byte 0xc0 + code
+
 /* Minimize conditionalization for different OS naming schemes */
 #ifdef DARWIN	
 #define GNAME(var) _##var
@@ -244,7 +249,9 @@ ENDFUNC(sse_restore)
  * The undefined-function trampoline.
  */
 FUNCDEF(undefined_tramp)
-	INT3
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
 	.byte	trap_Error
         /* Number of argument bytes */
         .byte   2
@@ -286,8 +293,17 @@ multiple_value_return:
 	
 	.globl GNAME(function_end_breakpoint_trap)
 GNAME(function_end_breakpoint_trap):
-	UD2
-	.byte 	trap_FunctionEndBreakpoint
+	/*
+	ud1	0(%ecx), %ecx
+	ud1	%ecx, %edx
+	.byte	0x0f
+	.byte	0xb9
+	.byte 	0xc0 + trap_PendingInterrupt
+	*/
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
+	.byte 	0xc0 + trap_FunctionEndBreakpoint
 	hlt			# Should never return here.
 
 	.globl GNAME(function_end_breakpoint_end)
@@ -295,14 +311,18 @@ GNAME(function_end_breakpoint_end):
 
 
 FUNCDEF(do_pending_interrupt)
-	ud2
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
 	.byte 	trap_PendingInterrupt
 	ret
 ENDFUNC(do_pending_interrupt)
 	
 #ifdef trap_DynamicSpaceOverflowError
 FUNCDEF(do_dynamic_space_overflow_error)
-	ud2
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
 	.byte 	trap_DynamicSpaceOverflowError
 	ret
 ENDFUNC(do_dynamic_space_overflow_error)
@@ -310,7 +330,9 @@ ENDFUNC(do_dynamic_space_overflow_error)
 	
 #ifdef trap_DynamicSpaceOverflowWarning
 FUNCDEF(do_dynamic_space_overflow_warning)
-	ud2
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
 	.byte 	trap_DynamicSpaceOverflowWarning
 	ret
 ENDFUNC(do_dynamic_space_overflow_warning)
@@ -493,7 +515,9 @@ FUNCDEF(undefined_foreign_symbol_trap)
         movl 8(%ebp),%eax
 
 	/* Now trap to Lisp */
-	ud2
+	# UD1
+	.byte	0x0f
+	.byte	0xb9
 	.byte	trap_Error
         /* Number of argument bytes */
         .byte   2



View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/93d7cf02396314c79a176f1a2c4208d5f3a52e21

-- 
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/93d7cf02396314c79a176f1a2c4208d5f3a52e21
You're receiving this email because of your account on gitlab.common-lisp.net.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cmucl-cvs/attachments/20210409/c0275219/attachment-0001.html>


More information about the cmucl-cvs mailing list