internal ACPIObject Evaluate(IMachineInterface mi, Namespace.State s, Namespace n) { if (this == null) { throw new Exception("passed null pointer as this"); } /* Evaluate as much as we can */ switch (Type) { case DataType.Arg: return(s.Args[(int)Data]); case DataType.Buffer: return(this); case DataType.BufferField: { BufferFieldData bfd = Data as BufferFieldData; ACPIObject new_buf = bfd.Buffer.EvaluateTo(DataType.Buffer, mi, s, n); if (bfd.BitOffset % 8 != 0) { throw new NotImplementedException("BitOffset not divisible by 8"); } if (bfd.BitLength % 8 != 0) { throw new NotImplementedException("BitLength not divisible by 8"); } int byte_offset = bfd.BitOffset / 8; int byte_length = bfd.BitLength / 8; byte[] ret = new byte[byte_length]; byte[] src = (byte[])new_buf.Data; for (int i = 0; i < byte_length; i++) { ret[i] = src[byte_offset + i]; } return(new ACPIObject(DataType.Buffer, ret)); } case DataType.DDBHandle: throw new NotImplementedException("DDBHandle"); case DataType.Device: return(this); case DataType.Event: return(this); case DataType.FieldUnit: if (Data is FieldUnitData) { FieldUnitData fud = Data as FieldUnitData; ACPIObject op_region = fud.OpRegion; if (op_region.Type != DataType.OpRegion) { throw new Exception("Read from FieldUnit with invalid OpRegion type (" + op_region.Type.ToString() + ")"); } OpRegionData ord = op_region.Data as OpRegionData; /* Ensure the requested field index is within the opregion */ int byte_offset = fud.BitOffset / 8; int byte_length = fud.BitLength / 8; if ((ulong)byte_offset + (ulong)byte_length > ord.Length) { throw new Exception("Read: attempt to read to field beyond length of opregion (offset: " + byte_offset.ToString() + ", length: " + byte_length.ToString() + ", OpRegion.Length: " + ord.Length + ")"); } if (fud.BitOffset % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned offset (" + fud.BitOffset.ToString() + ")"); } if (fud.BitLength % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned length (" + fud.BitLength.ToString() + ")"); } /* Do the read depending on the op region type */ switch (ord.RegionSpace) { case 0: // Memory switch (byte_length) { case 1: return(mi.ReadMemoryByte(ord.Offset + (ulong)byte_offset)); case 2: return(mi.ReadMemoryWord(ord.Offset + (ulong)byte_offset)); case 4: return(mi.ReadMemoryDWord(ord.Offset + (ulong)byte_offset)); case 8: return(mi.ReadMemoryQWord(ord.Offset + (ulong)byte_offset)); default: throw new NotImplementedException("Read: unsupported byte length: " + byte_length.ToString()); } case 1: // IO switch (byte_length) { case 1: return(mi.ReadIOByte(ord.Offset + (ulong)byte_offset)); case 2: return(mi.ReadIOWord(ord.Offset + (ulong)byte_offset)); case 4: return(mi.ReadIODWord(ord.Offset + (ulong)byte_offset)); case 8: return(mi.ReadIOQWord(ord.Offset + (ulong)byte_offset)); default: throw new NotImplementedException("Read: unsupported byte length: " + byte_length.ToString()); } case 2: // PCI Configuration space { // try and get the _ADR object for the current device ACPIObject adr = n.Evaluate(ord.Device.ToString() + "._ADR", mi).EvaluateTo(DataType.Integer, mi, s, n); if (adr == null) { throw new Exception(ord.Device.ToString() + "._ADR failed"); } uint bus = 0; uint device = ((uint)adr.IntegerData >> 16) & 0xffffU; uint func = (uint)adr.IntegerData & 0xffffU; uint offset = (uint)ord.Offset + (uint)byte_offset; System.Diagnostics.Debugger.Log(0, "acpipc", "Read from PCI conf space for: " + ord.Device.ToString() + ", _ADR: (" + adr.Type.ToString() + ") " + adr.IntegerData.ToString("X")); switch (byte_length) { case 1: return(mi.ReadPCIByte(bus, device, func, offset)); case 2: return(mi.ReadPCIWord(bus, device, func, offset)); case 4: return(mi.ReadPCIDWord(bus, device, func, offset)); default: throw new NotImplementedException("Read: unsupported byte length: " + byte_length.ToString()); } } default: throw new NotImplementedException("Read: unsupported OpRegion type: " + ord.ToString()); } } else if (Data is IndexFieldUnitData) { var ifud = Data as IndexFieldUnitData; /* Ensure the requested field index is byte aligned */ int byte_offset = ifud.BitOffset / 8; int byte_length = ifud.BitLength / 8; if (ifud.BitOffset % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned offset (" + ifud.BitOffset.ToString() + ")"); } if (ifud.BitLength % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned length (" + ifud.BitLength.ToString() + ")"); } /* Write to index port, read from data port */ ifud.Index.Write((ulong)byte_offset, mi, s, n); var ret = ifud.Data.Evaluate(mi, s, n); switch (byte_length) { case 1: ret.Data = ret.IntegerData & 0xffU; break; case 2: ret.Data = ret.IntegerData & 0xffffU; break; } return(ret); } throw new NotSupportedException("Invalid FieldUnit data type: " + Data.ToString()); case DataType.Integer: return(this); case DataType.Local: return(s.Locals[(int)Data]); case DataType.Method: { Namespace.State new_state = new Namespace.State(); if (s.IsMethodExec) { new_state.Args = s.Args; } else { new_state.Args = new Dictionary <int, ACPIObject>(new tysos.Program.MyGenericEqualityComparer <int>()); } new_state.Locals = new Dictionary <int, ACPIObject>(new tysos.Program.MyGenericEqualityComparer <int>()); ACPIObject ret; new_state.Scope = Name; MethodData md = Data as MethodData; int midx = md.Offset; //System.Diagnostics.Debugger.Log(0, "acpipc", "Begin evaluating method: " + Name.ToString()); if (md == null) { throw new Exception("Method object has no MethodData"); } if (md.Builtin != null) { return(md.Builtin.Execute(new_state)); } if (!n.ParseTermList(md.AML, ref midx, md.Length, out ret, new_state)) { throw new Exception(); } return(ret); } case DataType.Mutex: return(this); case DataType.ObjectReference: //System.Diagnostics.Debugger.Log(0, "acpipc", "Begin evaluating object reference: " + ((ACPIObject.ObjRefData)Data).ToString()); return(this); case DataType.OpRegion: return(this); case DataType.Package: return(this); case DataType.PowerResource: return(this); case DataType.Processor: return(this); case DataType.String: return(this); case DataType.ThermalZone: return(this); case DataType.Uninitialized: return(this); default: throw new NotImplementedException(); } }
internal void Write(ACPIObject d, int Offset, IMachineInterface mi, Namespace.State s, Namespace n) { if (Type == DataType.Uninitialized || (Type == DataType.ObjectReference && ((ObjRefData)Data).Object == null) || (Type == DataType.Integer && (ulong)Data == 0UL)) { return; } //System.Diagnostics.Debugger.Log(0, "acpipc", "Write: " + d.Type.ToString() + " to " + Type.ToString()); switch (Type) { case DataType.Local: s.Locals[(int)Data] = d; return; case DataType.Arg: s.Args[(int)Data] = d; return; case DataType.Buffer: { ACPIObject new_buf = d.EvaluateTo(DataType.Buffer, mi, s, n); byte[] dst = (byte[])Data; byte[] src = (byte[])new_buf.Data; for (int i = Offset; i < Offset + dst.Length; i++) { if (i < src.Length) { dst[i] = src[i]; } else { dst[i] = 0; } } return; } case DataType.ObjectReference: { ObjRefData ord = Data as ObjRefData; ACPIObject dst = ord.Object; switch (dst.Type) { case DataType.Buffer: dst.Write(d, ord.Index, mi, s, n); return; case DataType.BufferField: dst.Write(d, mi, s, n); return; case DataType.FieldUnit: dst.Write(d, mi, s, n); return; case DataType.Integer: dst.Data = d.EvaluateTo(DataType.Integer, mi, s, n).IntegerData; return; default: throw new NotSupportedException("Write: " + d.Type + " to ObjectReference(" + dst.Type + ")"); } } case DataType.BufferField: { BufferFieldData bfd = Data as BufferFieldData; ACPIObject new_buf_src = d.EvaluateTo(DataType.Buffer, mi, s, n); ACPIObject new_buf_dst = bfd.Buffer.EvaluateTo(DataType.Buffer, mi, s, n); if (bfd.BitOffset % 8 != 0) { throw new NotImplementedException("BitOffset not divisible by 8"); } if (bfd.BitLength % 8 != 0) { throw new NotImplementedException("BitLength not divisible by 8"); } int byte_offset = bfd.BitOffset / 8; int byte_length = bfd.BitLength / 8; byte[] src = (byte[])new_buf_src.Data; byte[] dst = (byte[])new_buf_dst.Data; for (int i = 0; i < byte_length; i++) { dst[byte_offset + i] = src[i]; } return; } case DataType.FieldUnit: if (Data is FieldUnitData) { FieldUnitData fud = Data as FieldUnitData; ACPIObject op_region = fud.OpRegion; if (op_region.Type != DataType.OpRegion) { throw new Exception("Write to FieldUnit with invalid OpRegion type (" + op_region.Type.ToString() + ")"); } OpRegionData ord = op_region.Data as OpRegionData; /* Ensure the requested field index is within the opregion */ int byte_offset = fud.BitOffset / 8; int byte_length = fud.BitLength / 8; if ((ulong)byte_offset + (ulong)byte_length > ord.Length) { throw new Exception("Write: attempt to write to field beyond length of opregion (offset: " + byte_offset.ToString() + ", length: " + byte_length.ToString() + ", OpRegion.Length: " + ord.Length + ")"); } if (fud.BitOffset % 8 != 0) { throw new NotImplementedException("Write: non-byte aligned offset (" + fud.BitOffset.ToString() + ")"); } if (fud.BitLength % 8 != 0) { throw new NotImplementedException("Write: non-byte aligned length (" + fud.BitLength.ToString() + ")"); } /* Get the data */ ulong int_val = d.EvaluateTo(DataType.Integer, mi, s, n).IntegerData; /* Do the write depending on the op region type */ switch (ord.RegionSpace) { case 0: // Memory switch (byte_length) { case 1: mi.WriteMemoryByte(ord.Offset + (ulong)byte_offset, (byte)(int_val & 0xff)); return; case 2: mi.WriteMemoryWord(ord.Offset + (ulong)byte_offset, (ushort)(int_val & 0xffff)); return; case 4: mi.WriteMemoryDWord(ord.Offset + (ulong)byte_offset, (uint)(int_val & 0xffffff)); return; case 8: mi.WriteMemoryQWord(ord.Offset + (ulong)byte_offset, int_val); return; default: throw new NotImplementedException("Write: unsupported byte length: " + byte_length.ToString()); } case 1: // IO switch (byte_length) { case 1: mi.WriteIOByte(ord.Offset + (ulong)byte_offset, (byte)(int_val & 0xff)); return; case 2: mi.WriteIOWord(ord.Offset + (ulong)byte_offset, (ushort)(int_val & 0xffff)); return; case 4: mi.WriteIODWord(ord.Offset + (ulong)byte_offset, (uint)(int_val & 0xffffff)); return; case 8: mi.WriteIOQWord(ord.Offset + (ulong)byte_offset, int_val); return; default: throw new NotImplementedException("Write: unsupported byte length: " + byte_length.ToString()); } case 2: // PCI Configuration space { // try and get the _ADR object for the current device ACPIObject adr = n.Evaluate(ord.Device.ToString() + "._ADR", mi).EvaluateTo(DataType.Integer, mi, s, n); if (adr == null) { throw new Exception(ord.Device.ToString() + "._ADR failed"); } uint bus = 0; uint device = ((uint)adr.IntegerData >> 16) & 0xffffU; uint func = (uint)adr.IntegerData & 0xffffU; uint offset = (uint)ord.Offset + (uint)byte_offset; switch (byte_length) { case 1: mi.WritePCIByte(bus, device, func, offset, (byte)int_val); return; case 2: mi.WritePCIWord(bus, device, func, offset, (ushort)int_val); return; case 4: mi.WritePCIDWord(bus, device, func, offset, (uint)int_val); return; default: throw new NotImplementedException("Write: unsupported byte length: " + byte_length.ToString()); } } default: throw new NotImplementedException("Write: unsupported OpRegion type: " + ord.ToString()); } } else if (Data is IndexFieldUnitData) { var ifud = Data as IndexFieldUnitData; /* Ensure the requested field index is byte aligned */ int byte_offset = ifud.BitOffset / 8; int byte_length = ifud.BitLength / 8; if (ifud.BitOffset % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned offset (" + ifud.BitOffset.ToString() + ")"); } if (ifud.BitLength % 8 != 0) { throw new NotImplementedException("Read: non-byte aligned length (" + ifud.BitLength.ToString() + ")"); } /* Get the data */ ulong int_val = d.EvaluateTo(DataType.Integer, mi, s, n).IntegerData; /* Write to index port, write to data port */ ifud.Index.Write((ulong)byte_offset, mi, s, n); ifud.Data.Write(int_val, mi, s, n); return; } else { throw new NotSupportedException("Invalid FieldUnit data type: " + Data.ToString()); } default: throw new NotImplementedException("Write: " + Type.ToString()); } throw new NotImplementedException(); }