Ver código fonte

wm: remap content canvases after the frame begins

The WM mapped window contents before u_wm_begin(), so a client
self-resize in that gap could unmap the front canvas under the draw.
usurf_canvas() then read released pages and the WM died with a #PF
(cr2=0x59420000, content slot 1).

Re-read each window's content generation after u_wm_begin(), when the
kernel refuses further self-resizes, and remap changed canvases in all
draw paths: full and partial redraw, geometry changes and drag.
Johannes Zellner 1 dia atrás
pai
commit
7f6dbd12ae
3 arquivos alterados com 47 adições e 2 exclusões
  1. 5 1
      AGENTS.md
  2. 5 1
      docs/graphics.html
  3. 37 0
      user/apps/wm/ucmd_wm.c

+ 5 - 1
AGENTS.md

@@ -606,7 +606,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   counters, so the WM can never lose one.  `content_gen` comes from a global
   counter and is unique per window creation, so a close followed by a new
   window in the same slot forces a remap instead of reusing a stale front
-  canvas mapping.  Closing a window kills its owner;
+  canvas mapping.  The ring-3 WM refreshes `SYS_WM_INFO`/`SYS_WM_WIN_MAP`
+  before opening a frame and revalidates each content generation again after
+  `u_wm_begin()` (`remap_window_contents()`): a client self-resize between
+  the pre-frame refresh and the frame begin used to unmap the canvas under
+  the draw and kill the WM with a #PF.  Closing a window kills its owner;
   the reaper frees the canvas.  `clock`, `colors`, `settime`,
   `term`, `files` and the other apps are canvas clients.  The old
   `SYS_GFX_BEGIN/PRESENT/END` terminal-takeover service and the kernel

+ 5 - 1
docs/graphics.html

@@ -309,7 +309,11 @@
 			counters, so the WM cannot lose one. <code>content_gen</code> comes
 			from a global counter and is unique per window creation, so a new
 			window that reuses a just-closed slot forces the WM to remap its
-			front canvas instead of reusing a stale mapping.
+			front canvas instead of reusing a stale mapping. The ring-3 WM
+			revalidates the generation after <code>u_wm_begin()</code>:
+			the kernel refuses client self-resizes while a frame is active,
+			so a resize between the pre-frame map refresh and the draw can no
+			longer unmap the canvas under the WM.
 		</p>
 		<p>
 			<code>SYS_WIN_CREATE</code> creates a generic canvas window.

+ 37 - 0
user/apps/wm/ucmd_wm.c

@@ -71,6 +71,7 @@ static int boot_scan_pid;
 static int boot_scan_placed;
 static unsigned int boot_scan_deadline;
 static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
+static void remap_window_contents(void);
 static int bar_temp_c = -1;
 static int bar_batt_pct = -1;
 static int bar_batt_charging;
@@ -1210,6 +1211,7 @@ static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 
 	if (u_wm_begin() != 0)
 		return;
+	remap_window_contents();
 	if (old_on && new_on)
 		usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
 	else
@@ -1252,6 +1254,7 @@ static void redraw_geom(struct uwin *u, int ox, int oy,
 	add_rect(&present, ox, oy, ow, oh);
 	add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
 	if (u_wm_begin() == 0) {
+		remap_window_contents();
 		for (i = 0; i < present.count; i++)
 			draw_rect(present.rect[i].x, present.rect[i].y,
 				  present.rect[i].w, present.rect[i].h);
@@ -1637,6 +1640,37 @@ static void sm_close(void)
 	full_redraw = 1;
 }
 
+/* A client can self-resize between the pre-frame info/map refresh and
+ * u_wm_begin(); that unmaps and remaps its content slot, leaving the map
+ * cached above pointing at released pages.  Once the frame is active the
+ * kernel refuses further self-resizes, so re-reading the generation and
+ * remapping here makes the cached canvas safe for the draw calls.  The
+ * window info itself is left for the next loop, which then sees the
+ * generation change and does a full redraw. */
+static void remap_window_contents(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < WM_MAX_WINDOWS; i++) {
+		struct wm_window_info info;
+		struct wm_map_info map;
+
+		if (u_wm_info(i, &info) != 1 || !info.used) {
+			wins[i].mapped = 0;
+			continue;
+		}
+		if (wins[i].mapped &&
+		    wins[i].map.generation == info.content_gen)
+			continue;
+		if (u_wm_map(i, &map) == 0) {
+			wins[i].map = map;
+			wins[i].mapped = 1;
+		} else {
+			wins[i].mapped = 0;
+		}
+	}
+}
+
 static void do_update(struct wm_status *st)
 {
 	struct wm_present_info present;
@@ -1897,6 +1931,7 @@ static void do_update(struct wm_status *st)
 			return;
 		}
 		full_redraw = 0;
+		remap_window_contents();
 		need = 1;
 		{
 			draw_rect(0, 0, fbw, fbh);
@@ -1964,6 +1999,7 @@ static void do_update(struct wm_status *st)
 		sm_add_present(&present);
 	if (u_wm_begin() != 0)
 		return;
+	remap_window_contents();
 
 	for (i = 0; i < present.count; i++)
 		draw_rect(present.rect[i].x, present.rect[i].y,
@@ -2390,6 +2426,7 @@ static void handle_mouse(struct wm_status *st)
 						add_rect(&present, nx, ny,
 							 u->info.w, u->info.h);
 						if (u_wm_begin() == 0) {
+							remap_window_contents();
 							for (i = 0;
 							     i < present.count; i++)
 								draw_rect(