sencha: add support for exposing the cheriot_mpact simulator cli

The cheriot_mpact command line can be (optionally) enabled on an inet
socket (default port# 4567). When enabled cpu1 will not run (and renode
will block) until a "run" command is issued on the cli.

NB: this requires an updated cheriot toolchain

Change-Id: Idcdcadd09703c03a2f71f656b49f065bfec786e1
diff --git a/sencha.resc b/sencha.resc
index a9ced9d..ab84cc2 100644
--- a/sencha.resc
+++ b/sencha.resc
@@ -60,6 +60,10 @@
 $cheriotLibrary ?= @out/cheriot/sim/librenode_mpact_cheriot.so
 sysbus.cpu1 CpuLibraryPath $cheriotLibrary
 
+# Enable the command line interface on port 4567
+$cli_port?= 4567
+#sysbus.cpu1 CliPort $cli_port
+
 # Setup ram_smc to coordinate with cpu1
 sysbus.ram_smc CpuLibraryPath $cheriotLibrary
 sysbus.ram_smc Start
diff --git a/shodan_infrastructure/MpactCheriotCPU.cs b/shodan_infrastructure/MpactCheriotCPU.cs
index 20161b4..3d2f0de 100644
--- a/shodan_infrastructure/MpactCheriotCPU.cs
+++ b/shodan_infrastructure/MpactCheriotCPU.cs
@@ -68,7 +68,7 @@
 		public MpactCheriotCPU(uint id, UInt64 memoryBase, UInt64 memorySize,
 				       UInt64 revocationMemoryBase, UInt64 clintMMRBase,
 				       string cpuType, IMachine machine, Endianess endianness,
-               CpuBitness bitness = CpuBitness.Bits32)
+				       CpuBitness bitness = CpuBitness.Bits32)
 		: base(id, cpuType, machine, endianness, bitness)
 		{
 			this.memoryBase = memoryBase;
@@ -79,7 +79,6 @@
 			value_ptr = Marshal.AllocHGlobal(8);
 			reg_info_ptr = Marshal.AllocHGlobal(Marshal.SizeOf<RegInfo>());
 			string_ptr = Marshal.AllocHGlobal(maxStringLen);
-			run_cb_delegate = new ActionInt32(RunComplete);
 			read_sysmem_delegate = new FuncInt32UInt64IntPtrInt32(ReadSysMemory);
 			write_sysmem_delegate = new FuncInt32UInt64IntPtrInt32(WriteSysMemory);
 			cpuLibraryRegistered = false;
@@ -110,13 +109,21 @@
 				return;
 			}
 			// Set up configuration array.
-			var config_names = new string[4] {"memoryBase", "memorySize", "revocationMemoryBase", "clintMMRBase"};
-			var config_values = new string[4];
-			config_values[0] = "0x" + memoryBase.ToString("X");
-			config_values[1] = "0x" + memorySize.ToString("X");
-			config_values[2] = "0x" + revocationMemBase.ToString("X");
-			config_values[3] = "0x" + clintBase.ToString("X");
-			Int32 cfg_res = set_config(mpact_id, config_names, config_values, 4);
+			var config_names = new List<string>();
+			var config_values = new List<string>();
+			config_names.Add("memoryBase");
+			config_names.Add("memorySize");
+			config_names.Add("revocationMemoryBase");
+			config_names.Add("clintMMRBase");
+			config_values.Add("0x" + memoryBase.ToString("X"));
+			config_values.Add("0x" + memorySize.ToString("X"));
+			config_values.Add("0x" + revocationMemBase.ToString("X"));
+			config_values.Add("0x" + clintBase.ToString("X"));
+			if (cli_port != -1) {
+				config_names.Add("cliPort");
+				config_values.Add("0x" + cli_port.ToString("X"));
+			}
+			Int32 cfg_res = set_config(mpact_id, config_names.ToArray(), config_values.ToArray(), config_names.Count);
 			if (cfg_res < 0) {
 				LogAndThrowRE("Failed to set configuration information");
 			}
@@ -134,8 +141,6 @@
 		public override void Dispose()
 		{
 			base.Dispose();
-			// Cleanup: simulator and any unmanaged resources.
-			halt(mpact_id, error_ptr);
 			destruct(mpact_id);
 		}
 
@@ -173,6 +178,8 @@
 				}
 		}
 
+		public int CliPort { get => cli_port; set => cli_port = value; }
+
 		public override ulong ExecutedInstructions => totalExecutedInstructions;
 
 		public override ExecutionMode ExecutionMode
@@ -341,15 +348,6 @@
 		return load_image(mpact_id, file_name, address);
 	}
 
-	public void RunComplete(Int32 result) {
-		Console.Error.WriteLine($"RunComplete({result})");
-	}
-
-	public void Run() {
-		Int32 status = run(mpact_id, run_cb_delegate);
-		Console.Error.WriteLine($"Run -> {status}");
-	}
-
 	public void OnGPIO(int number, bool value) {
 		if (mpact_id <= 0) {
 			this.Log(LogLevel.Noisy, "OnGPIO: no simulator, discard gpio {0}:{1}", number, value);
@@ -493,8 +491,7 @@
 		private Dictionary<string, Int32> registerNamesMap = new Dictionary<string, Int32>();
 		private string cpuLibraryPath;
     private bool cpuLibraryRegistered;
-		private string executableFile;
-		private string simulationFilePath;
+		private int cli_port = -1;
 		private NativeBinder binder;
 		private readonly object nativeLock;
 		private readonly Int32 maxStringLen = 32;
@@ -502,7 +499,6 @@
 		private IntPtr error_ptr {get; set;}
 		private IntPtr reg_info_ptr {get; set;}
 		private IntPtr string_ptr {get; set;}
-		private ActionInt32 run_cb_delegate;
 		private FuncInt32UInt64IntPtrInt32 read_sysmem_delegate;
 		private FuncInt32UInt64IntPtrInt32 write_sysmem_delegate;
 #pragma warning disable 649
@@ -532,10 +528,6 @@
 		private FuncInt32Int32UInt64IntPtrInt32 read_memory; // (Int32 id, UInt64 address, IntPtr buffer, Int32 size);
 		[Import(UseExceptionWrapper = false)]
 		private FuncInt32Int32UInt64IntPtrInt32 write_memory; // (Int32 id, UInt64 address, IntPtr buffer, Int32 size);
-		[Import(UseExceptionWrapper = false)]
-		private FuncInt32Int32ActionInt32 run; // (Int32 id, void callback(Int32) )
-		[Import(UseExceptionWrapper = false)]
-		private FuncInt32Int32IntPtr halt; // (Int32 id, IntPtr *value)
 		// Set config.
 		[Import(UseExceptionWrapper = false)]
 		private FuncInt32Int32StringArrStringArrInt32 set_config; // Int32 set_config(Int32 id, string[] names string[] values, Int32 size);