ia64/xen-unstable

changeset 1974:fc03d5fc3686

bitkeeper revision 1.1098.1.7 (4108b4e19yj8s3PBFv-lUhzWOuLMhQ)

General refactoring.
author sos22@donkeykong.cl.cam.ac.uk
date Thu Jul 29 08:27:13 2004 +0000 (2004-07-29)
parents fcf4e9746724
children c1e2ec69b1db
files xen/arch/x86/xdb.c
line diff
     1.1 --- a/xen/arch/x86/xdb.c	Wed Jul 28 16:59:33 2004 +0000
     1.2 +++ b/xen/arch/x86/xdb.c	Thu Jul 29 08:27:13 2004 +0000
     1.3 @@ -11,21 +11,24 @@
     1.4     debugger. so avoid it. */
     1.5  #define dbg_printk(...)
     1.6  
     1.7 -static int
     1.8 -xendbg_serhnd = -1;
     1.9 +struct xendbg_context {
    1.10 +	int serhnd;
    1.11 +	u8 reply_csum;
    1.12 +};
    1.13  
    1.14  static void
    1.15 -xendbg_put_char(u8 data)
    1.16 +xendbg_put_char(u8 data, struct xendbg_context *ctx)
    1.17  {
    1.18 -	serial_putc(xendbg_serhnd, data);
    1.19 +	ctx->reply_csum += data;
    1.20 +	serial_putc(ctx->serhnd, data);
    1.21  }
    1.22  
    1.23  static u8
    1.24 -xendbg_get_char(void)
    1.25 +xendbg_get_char(struct xendbg_context *ctx)
    1.26  {
    1.27  	u8 ch;
    1.28  	extern unsigned char __serial_getc(int handle);
    1.29 -	ch = __serial_getc(xendbg_serhnd);
    1.30 +	ch = __serial_getc(ctx->serhnd);
    1.31  	return ch;
    1.32  }
    1.33  
    1.34 @@ -46,7 +49,7 @@ hex_char_val(unsigned char c)
    1.35  /* Receive a command.  Returns -1 on csum error, 0 otherwise. */
    1.36  /* Does not acknowledge. */
    1.37  static int
    1.38 -attempt_receive_packet(char *recv_buf)
    1.39 +attempt_receive_packet(char *recv_buf, struct xendbg_context *ctx)
    1.40  {
    1.41  	int count;
    1.42  	u8 csum;
    1.43 @@ -54,11 +57,11 @@ attempt_receive_packet(char *recv_buf)
    1.44  	u8 ch;
    1.45  
    1.46  	/* Skip over everything up to the first '$' */
    1.47 -	while ((ch = xendbg_get_char()) != '$')
    1.48 +	while ((ch = xendbg_get_char(ctx)) != '$')
    1.49  		;
    1.50  	csum = 0;
    1.51  	for (count = 0; count < 4096; count++) {
    1.52 -		ch = xendbg_get_char();
    1.53 +		ch = xendbg_get_char(ctx);
    1.54  		if (ch == '#')
    1.55  			break;
    1.56  		recv_buf[count] = ch;
    1.57 @@ -69,8 +72,8 @@ attempt_receive_packet(char *recv_buf)
    1.58  		return -1;
    1.59  	}
    1.60  	recv_buf[count] = 0;
    1.61 -	received_csum = hex_char_val(xendbg_get_char()) * 16 +
    1.62 -		hex_char_val(xendbg_get_char());
    1.63 +	received_csum = hex_char_val(xendbg_get_char(ctx)) * 16 +
    1.64 +		hex_char_val(xendbg_get_char(ctx));
    1.65  	if (received_csum == csum) {
    1.66  		return 0;
    1.67  	} else {
    1.68 @@ -80,183 +83,138 @@ attempt_receive_packet(char *recv_buf)
    1.69  
    1.70  /* Send a string of bytes to the debugger. */
    1.71  static void
    1.72 -xendbg_send(const char *buf, int count)
    1.73 +xendbg_send(const char *buf, int count, struct xendbg_context *ctx)
    1.74  {
    1.75  	int x;
    1.76  	for (x = 0; x < count; x++)
    1.77 -		xendbg_put_char(buf[x]);
    1.78 +		xendbg_put_char(buf[x], ctx);
    1.79  }
    1.80  
    1.81  /* Receive a command, discarding up to ten packets with csum
    1.82   * errors.  Acknowledges all received packets. */
    1.83  static int
    1.84 -receive_command(char *recv_buf)
    1.85 +receive_command(char *recv_buf, struct xendbg_context *ctx)
    1.86  {
    1.87  	int r;
    1.88  	int count;
    1.89  
    1.90  	count = 0;
    1.91  	do {
    1.92 -		r = attempt_receive_packet(recv_buf);
    1.93 +		r = attempt_receive_packet(recv_buf, ctx);
    1.94  		if (r < 0)
    1.95 -			xendbg_send("-", 1);
    1.96 +			xendbg_put_char('-', ctx);
    1.97  		else
    1.98 -			xendbg_send("+", 1);
    1.99 +			xendbg_put_char('+', ctx);
   1.100  		count++;
   1.101  	} while (r < 0 && count < 10);
   1.102  	return r;
   1.103  }
   1.104  
   1.105  static void
   1.106 -u32_to_hex_u8(unsigned char val, char *buf)
   1.107 -{
   1.108 -	sprintf(buf, "%.02x\n", val);
   1.109 -}
   1.110 -
   1.111 -static void
   1.112 -u32_to_hex_u32(unsigned val, char *buf)
   1.113 -{
   1.114 -	sprintf(buf, "%.08x\n", val);
   1.115 -}
   1.116 -
   1.117 -static void
   1.118 -xendbg_send_hex_u8(unsigned char val)
   1.119 +xendbg_start_reply(struct xendbg_context *ctx)
   1.120  {
   1.121 -	char buf[3];
   1.122 -	u32_to_hex_u8(val, buf);
   1.123 -	xendbg_send(buf, 2);
   1.124 -}
   1.125 -
   1.126 -static u8
   1.127 -xendbg_reply_csum;
   1.128 -
   1.129 -static void
   1.130 -xendbg_start_reply(void)
   1.131 -{
   1.132 -	xendbg_reply_csum = 0;
   1.133 -	xendbg_send("$", 1);
   1.134 -}
   1.135 -
   1.136 -static void
   1.137 -xendbg_sendrep_data(const unsigned char *data, unsigned long len)
   1.138 -{
   1.139 -	int x;
   1.140 -
   1.141 -	for (x = 0; x < len; x++) {
   1.142 -		xendbg_put_char(data[x]);
   1.143 -		xendbg_reply_csum += data[x];
   1.144 -	}
   1.145 +	xendbg_put_char('$', ctx);
   1.146 +	ctx->reply_csum = 0;
   1.147  }
   1.148  
   1.149  /* Return 0 if the reply was successfully received, !0 otherwise. */
   1.150  static int
   1.151 -xendbg_finish_reply(void)
   1.152 +xendbg_finish_reply(struct xendbg_context *ctx)
   1.153  {
   1.154  	char ch;
   1.155 +	char buf[3];
   1.156  
   1.157 -	xendbg_send("#", 1);
   1.158 -	xendbg_send_hex_u8(xendbg_reply_csum);
   1.159 -	ch = xendbg_get_char();
   1.160 +	sprintf(buf, "%.02x\n", ctx->reply_csum);
   1.161 +
   1.162 +	xendbg_put_char('#', ctx);
   1.163 +	xendbg_send(buf, 2, ctx);
   1.164 +
   1.165 +	ch = xendbg_get_char(ctx);
   1.166  	if (ch == '+')
   1.167  		return 0;
   1.168  	else
   1.169  		return 1;
   1.170  }
   1.171  
   1.172 -static void
   1.173 -xendbg_sendrep_hex_u8(unsigned val)
   1.174 +/* Swap the order of the bytes in a work. */
   1.175 +static inline unsigned
   1.176 +bswab32(unsigned val)
   1.177  {
   1.178 -	char buf[3];
   1.179 -	u32_to_hex_u8(val, buf);
   1.180 -	xendbg_sendrep_data(buf, 2);
   1.181 -}
   1.182 -
   1.183 -static void
   1.184 -xendbg_sendrep_hex_u32(unsigned val)
   1.185 -{
   1.186 -	char buf[9];
   1.187 -	u32_to_hex_u32(val, buf);
   1.188 -	xendbg_sendrep_data(buf, 8);
   1.189 -}
   1.190 -
   1.191 -static void
   1.192 -xendbg_sendrep_hex_u32_le(unsigned val)
   1.193 -{
   1.194 -	val = (((val >> 0) & 0xff) << 24) |
   1.195 +	return (((val >> 0) & 0xff) << 24) |
   1.196  		(((val >> 8) & 0xff) << 16) |
   1.197  		(((val >> 16) & 0xff) << 8) |
   1.198  		(((val >> 24) & 0xff) << 0);
   1.199 -	xendbg_sendrep_hex_u32(val);
   1.200  }
   1.201  
   1.202  static int
   1.203 -handle_memory_read_command(unsigned long addr, unsigned long length)
   1.204 +handle_memory_read_command(unsigned long addr, unsigned long length,
   1.205 +			   struct xendbg_context *ctx)
   1.206  {
   1.207  	int x;
   1.208  	unsigned char val;
   1.209  	int r;
   1.210  	unsigned old_s_limit;
   1.211 +	char buf[2];
   1.212  
   1.213  	dbg_printk("Memory read starting at %lx, length %lx.\n", addr,
   1.214  		   length);
   1.215  	old_s_limit = current->addr_limit.seg;
   1.216  	current->addr_limit.seg = ~0;
   1.217 -	xendbg_start_reply();
   1.218 +	xendbg_start_reply(ctx);
   1.219  	for (x = 0; x < length; x++) {
   1.220  		r = copy_from_user(&val, (void *)(addr + x), 1);
   1.221  		if (r != 0) {
   1.222  			dbg_printk("Error reading from %lx.\n", addr + x);
   1.223  			break;
   1.224  		}
   1.225 -		xendbg_sendrep_hex_u8(val);
   1.226 +		sprintf(buf, "%.02x", val);
   1.227 +		xendbg_send(buf, 2, ctx);
   1.228  	}
   1.229  	if (x == 0)
   1.230 -		xendbg_sendrep_data("E05", 3);
   1.231 +		xendbg_send("E05", 3, ctx);
   1.232  	dbg_printk("Read done.\n");
   1.233  	current->addr_limit.seg = old_s_limit;
   1.234 -	return xendbg_finish_reply();
   1.235 +	return xendbg_finish_reply(ctx);
   1.236  }
   1.237  
   1.238  static int
   1.239 -xendbg_send_reply(const char *buf)
   1.240 +xendbg_send_reply(const char *buf, struct xendbg_context *ctx)
   1.241  {
   1.242 -	xendbg_start_reply();
   1.243 -	xendbg_sendrep_data(buf, strlen(buf));
   1.244 -	return xendbg_finish_reply();
   1.245 +	xendbg_start_reply(ctx);
   1.246 +	xendbg_send(buf, strlen(buf), ctx);
   1.247 +	return xendbg_finish_reply(ctx);
   1.248  }
   1.249  
   1.250  static int
   1.251 -handle_register_read_command(struct pt_regs *regs)
   1.252 +handle_register_read_command(struct pt_regs *regs, struct xendbg_context *ctx)
   1.253  {
   1.254 -	xendbg_start_reply();
   1.255 -	xendbg_sendrep_hex_u32_le(regs->eax);
   1.256 -	xendbg_sendrep_hex_u32_le(regs->ecx);
   1.257 -	xendbg_sendrep_hex_u32_le(regs->edx);
   1.258 -	xendbg_sendrep_hex_u32_le(regs->ebx);
   1.259 -	xendbg_sendrep_hex_u32_le(regs->esp);
   1.260 -	xendbg_sendrep_hex_u32_le(regs->ebp);
   1.261 -	xendbg_sendrep_hex_u32_le(regs->esi);
   1.262 -	xendbg_sendrep_hex_u32_le(regs->edi);
   1.263 -	xendbg_sendrep_hex_u32_le(regs->eip);
   1.264 -	xendbg_sendrep_hex_u32_le(regs->eflags);
   1.265 -	xendbg_sendrep_hex_u32_le(regs->xcs);
   1.266 -	xendbg_sendrep_hex_u32_le(regs->xss);
   1.267 -	xendbg_sendrep_hex_u32_le(regs->xes);
   1.268 -	xendbg_sendrep_hex_u32_le(regs->xfs);
   1.269 -	xendbg_sendrep_hex_u32_le(regs->xgs);
   1.270 -	return xendbg_finish_reply();
   1.271 -}
   1.272 +	char buf[121];
   1.273  
   1.274 -static unsigned long
   1.275 -hex_to_int(const char *start, const char **end)
   1.276 -{
   1.277 -	return simple_strtol(start, (char **)end, 16);
   1.278 +	sprintf(buf,
   1.279 +		"%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x%.08x",
   1.280 +		bswab32(regs->eax),
   1.281 +		bswab32(regs->ecx),
   1.282 +		bswab32(regs->edx),
   1.283 +		bswab32(regs->ebx),
   1.284 +		bswab32(regs->esp),
   1.285 +		bswab32(regs->ebp),
   1.286 +		bswab32(regs->esi),
   1.287 +		bswab32(regs->edi),
   1.288 +		bswab32(regs->eip),
   1.289 +		bswab32(regs->eflags),
   1.290 +		bswab32(regs->xcs),
   1.291 +		bswab32(regs->xss),
   1.292 +		bswab32(regs->xes),
   1.293 +		bswab32(regs->xfs),
   1.294 +		bswab32(regs->xgs));
   1.295 +	return xendbg_send_reply(buf, ctx);
   1.296  }
   1.297  
   1.298  static int
   1.299 -process_command(const char *received_packet, struct pt_regs *regs)
   1.300 +process_command(char *received_packet, struct pt_regs *regs,
   1.301 +		struct xendbg_context *ctx)
   1.302  {
   1.303 -	const char *ptr;
   1.304 +	char *ptr;
   1.305  	unsigned long addr, length;
   1.306  	int retry;
   1.307  	int counter;
   1.308 @@ -267,39 +225,40 @@ process_command(const char *received_pac
   1.309  	do {
   1.310  		switch (received_packet[0]) {
   1.311  		case 'g': /* Read registers */
   1.312 -			retry = handle_register_read_command(regs);
   1.313 +			retry = handle_register_read_command(regs, ctx);
   1.314  			break;
   1.315  		case 'm': /* Read memory */
   1.316 -			addr = hex_to_int(received_packet + 1, &ptr);
   1.317 +			addr = simple_strtoul(received_packet + 1, &ptr, 16);
   1.318  			if (ptr == received_packet + 1 ||
   1.319  			    ptr[0] != ',') {
   1.320 -				xendbg_send_reply("E03");
   1.321 +				xendbg_send_reply("E03", ctx);
   1.322  				return 0;
   1.323  			}
   1.324 -			length = hex_to_int(ptr + 1, &ptr);
   1.325 +			length = simple_strtoul(ptr + 1, &ptr, 16);
   1.326  			if (ptr[0] != 0) {
   1.327 -				xendbg_send_reply("E04");
   1.328 +				xendbg_send_reply("E04", ctx);
   1.329  				return 0;
   1.330  			}
   1.331  			retry =
   1.332  				handle_memory_read_command(addr,
   1.333 -							   length);
   1.334 +							   length,
   1.335 +							   ctx);
   1.336  			break;
   1.337  		case 'G': /* Write registers */
   1.338  		case 'M': /* Write memory */
   1.339 -			retry = xendbg_send_reply("E02");
   1.340 +			retry = xendbg_send_reply("E02", ctx);
   1.341  			break;
   1.342  		case 'D':
   1.343  			resume = 1;
   1.344 -			retry = xendbg_send_reply("");
   1.345 +			retry = xendbg_send_reply("", ctx);
   1.346  			break;
   1.347  		case 'c': /* Resume at current address */
   1.348  		case 's': /* Single step */
   1.349  		case '?':
   1.350 -			retry = xendbg_send_reply("S01");
   1.351 +			retry = xendbg_send_reply("S01", ctx);
   1.352  			break;
   1.353  		default:
   1.354 -			retry = xendbg_send_reply("");
   1.355 +			retry = xendbg_send_reply("", ctx);
   1.356  			break;
   1.357  		}
   1.358  		counter++;
   1.359 @@ -311,6 +270,11 @@ process_command(const char *received_pac
   1.360  	return resume;
   1.361  }
   1.362  
   1.363 +static struct xendbg_context
   1.364 +xdb_ctx = {
   1.365 +	serhnd : -1
   1.366 +};
   1.367 +
   1.368  void
   1.369  __trap_to_xendbg(struct pt_regs *regs)
   1.370  {
   1.371 @@ -320,7 +284,7 @@ void
   1.372  	static char recv_buf[4096];
   1.373  	unsigned flags;
   1.374  
   1.375 -	if (xendbg_serhnd < 0) {
   1.376 +	if (xdb_ctx.serhnd < 0) {
   1.377  		dbg_printk("Debugger not ready yet.\n");
   1.378  		return;
   1.379  	}
   1.380 @@ -343,12 +307,12 @@ void
   1.381  	local_irq_save(flags);
   1.382  
   1.383  	while (resume == 0) {
   1.384 -		r = receive_command(recv_buf);
   1.385 +		r = receive_command(recv_buf, &xdb_ctx);
   1.386  		if (r < 0) {
   1.387  			dbg_printk("GDB disappeared, trying to resume Xen...\n");
   1.388  			resume = 1;
   1.389  		} else
   1.390 -			resume = process_command(recv_buf, regs);
   1.391 +			resume = process_command(recv_buf, regs, &xdb_ctx);
   1.392  	}
   1.393  	xendbg_running = 0;
   1.394  	local_irq_restore(flags);
   1.395 @@ -361,12 +325,12 @@ initialize_xendbg(void)
   1.396  
   1.397  	if (!strcmp(opt_xendbg, "none"))
   1.398  		return;
   1.399 -	xendbg_serhnd = parse_serial_handle(opt_xendbg);
   1.400 -	if (xendbg_serhnd == -1)
   1.401 +	xdb_ctx.serhnd = parse_serial_handle(opt_xendbg);
   1.402 +	if (xdb_ctx.serhnd == -1)
   1.403  		panic("Can't parse %s as XDB serial info.\n", opt_xendbg);
   1.404  
   1.405  	/* Acknowledge any spurious GDB packets. */
   1.406 -	xendbg_put_char('+');
   1.407 +	xendbg_put_char('+', &xdb_ctx);
   1.408  
   1.409  	printk("Xendbg initialised.\n");
   1.410  }