radv: enable the trap handler and configure the shader exceptions

When TRAP_PRESENT is not enabled, all traps and exceptions are ignored.
Only EXCP_EN.mem_viol is currently supported because the other
exceptions have to be tested/validated first.

EXCP_EN.mem_viol is used to detect any sort of invalid memory
access like VM fault. When a memory violation is reported, the
hw jumps to the trap handler.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6384>
diff --git a/src/amd/vulkan/radv_shader.c b/src/amd/vulkan/radv_shader.c
index 8d0cd9d..5ca57e0 100644
--- a/src/amd/vulkan/radv_shader.c
+++ b/src/amd/vulkan/radv_shader.c
@@ -805,12 +805,13 @@
 	return code_size + DEBUGGER_NUM_MARKERS * 4;
 }
 
-static void radv_postprocess_config(const struct radv_physical_device *pdevice,
+static void radv_postprocess_config(const struct radv_device *device,
 				    const struct ac_shader_config *config_in,
 				    const struct radv_shader_info *info,
 				    gl_shader_stage stage,
 				    struct ac_shader_config *config_out)
 {
+	const struct radv_physical_device *pdevice = device->physical_device;
 	bool scratch_enabled = config_in->scratch_bytes_per_wave > 0;
 	unsigned vgpr_comp_cnt = 0;
 	unsigned num_input_vgprs = info->num_input_vgprs;
@@ -836,6 +837,15 @@
 	config_out->rsrc2 = S_00B12C_USER_SGPR(info->num_user_sgprs) |
 			    S_00B12C_SCRATCH_EN(scratch_enabled);
 
+	if (device->trap_handler_shader) {
+		/* Enable the trap handler if requested and configure the
+		 * shader exceptions like memory violation, etc.
+		 * TODO: Enable (and validate) more exceptions.
+		 */
+		config_out->rsrc2 |= S_00B12C_TRAP_PRESENT(1) |
+				     S_00B12C_EXCP_EN(1 << 8); /* mem_viol */
+	}
+
 	if (!pdevice->use_ngg_streamout) {
 		config_out->rsrc2 |= S_00B12C_SO_BASE0_EN(!!info->so.strides[0]) |
 				     S_00B12C_SO_BASE1_EN(!!info->so.strides[1]) |
@@ -1108,7 +1118,7 @@
 	}
 
 	variant->info = binary->info;
-	radv_postprocess_config(device->physical_device, &config, &binary->info,
+	radv_postprocess_config(device, &config, &binary->info,
 				binary->stage, &variant->config);
 
 	void *dest_ptr = radv_alloc_shader_memory(device, variant);