| 
									
										
										
										
											2019-05-14 11:07:15 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-08 22:42:52 +00:00
										 |  |  |  * ARM Compatible Semihosting Console Support. | 
					
						
							| 
									
										
										
										
											2019-05-14 11:07:15 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2019 Linaro Ltd | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-01-08 22:42:52 +00:00
										 |  |  |  * Currently ARM and RISC-V are unique in having support for | 
					
						
							|  |  |  |  * semihosting support in linux-user. So for now we implement the | 
					
						
							|  |  |  |  * common console API but just for arm and risc-v linux-user. | 
					
						
							| 
									
										
										
										
											2019-05-14 11:07:15 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: GPL-2.0-or-later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2021-03-05 13:54:49 +00:00
										 |  |  | #include "semihosting/console.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-14 11:07:15 +01:00
										 |  |  | #include "qemu.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-08 16:44:03 +01:00
										 |  |  | #include "user-internals.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  | #include <termios.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-14 11:07:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * For linux-user we can safely block. However as we want to return as | 
					
						
							|  |  |  |  * soon as a character is read we need to tweak the termio to disable | 
					
						
							|  |  |  |  * line buffering. We restore the old mode afterwards in case the | 
					
						
							|  |  |  |  * program is expecting more normal behaviour. This is slow but | 
					
						
							|  |  |  |  * nothing using semihosting console reading is expecting to be fast. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-05-01 12:31:08 -07:00
										 |  |  | int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-05-01 12:31:08 -07:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  |     struct termios old_tio, new_tio; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Disable line-buffering and echo */ | 
					
						
							|  |  |  |     tcgetattr(STDIN_FILENO, &old_tio); | 
					
						
							|  |  |  |     new_tio = old_tio; | 
					
						
							|  |  |  |     new_tio.c_lflag &= (~ICANON & ~ECHO); | 
					
						
							| 
									
										
										
										
											2022-05-01 12:31:08 -07:00
										 |  |  |     new_tio.c_cc[VMIN] = 1; | 
					
						
							|  |  |  |     new_tio.c_cc[VTIME] = 0; | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  |     tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-01 12:31:08 -07:00
										 |  |  |     ret = fread(buf, 1, len, stdin); | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* restore config */ | 
					
						
							|  |  |  |     tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-01 12:31:08 -07:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-11-04 12:42:30 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-05-01 12:42:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | int qemu_semihosting_console_write(void *buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return fwrite(buf, 1, len, stderr); | 
					
						
							|  |  |  | } |