gsp/620: frame the largest legal element and keep empty NVKV entries

The 620 framing carried one queue element's worth of RPC but validated the
length against the R580 limit (0xffd0), so the largest payload the 24-byte
header allows (0xffc8, rpc.length 0xffe8) was refused. Derive the 620 limit
from `queueElementSizeMax - queueElementHeaderSize` as the closed driver does
(`gspMsgQueueGetMaxRpcSize`), and test framing exactly at the limit.

`gmcapi::write_message` zeroed its header with a plain store, which LLVM can
lower to memset once the module is linked; use the same black_box form as the
R580 packer. The NVKV walker branched on "has following words", silently
dropping zero-count SEQ*/ARRAY* entries; branch on the opcode instead.

Also remove the duplicated oracle fields, correct the 0x18 header size in the
framing comments, and update the oracle's own tests for the two accepted 620
releases and the two new probe headers.

Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
kubuntugamer
2026-12-02 21:56:54 -06:00
parent fb8b272372
commit 504167838f
7 changed files with 104 additions and 18 deletions

View File

@ -6,7 +6,7 @@ Reference tree: `target/ref/ogkm`, `version.mk` `NVIDIA_VERSION = 620.73.04` —
on the RTX 5050 under test.
Oracle run: `OPEN_GPU_ROOT=target/ref/ogkm python3 tools/oracle_gsp_620.py` →
`passed: true, checked_fields: 107, diffs: []`.
`passed: true, checked_fields: 106, diffs: []`.
## 1. Machine-checked claims (oracle, C headers → our constants)
@ -57,7 +57,7 @@ Consequences for the RTX 5000 track:
## 4. WPR2 metadata (`GspFwWprMeta`) — machine-checked
The oracle probe now emits **107 fields** (was 29), all with 0 diffs against the 620.73.04 headers
The oracle probe now emits **106 fields** (was 29), all with 0 diffs against the 620.73.04 headers
including `gsp_fw_wpr_meta.h`:
- `sizeof(GspFwWprMeta) == 256`, and every field offset we serialize — magic `0x00`, revision

View File

@ -156,7 +156,7 @@ words for a GB207-shaped input), overflow reported not hidden, reply parse/trunc
of every opcode the encoder emits, a static-info reply covering the keys the boot consumes (plus an
unknown key that must be skipped), and malformed replies (bad opcode, count past the end).
The oracle grew to **107 fields, 0 diffs** against 620.73.04, now covering the `GMCAPI_HEADER` offsets
The oracle grew to **106 fields, 0 diffs** against 620.73.04, now covering the `GMCAPI_HEADER` offsets
(0/4/8, reserved 0x14, data 0x28), the id mask, the 30 `NVGMC_SI_*` keys this module emits and the 12
`NVGMC_SC_*` keys it decodes.

View File

@ -1154,8 +1154,8 @@ pub fn frame_element(
// ---- element framing per GSP-RM wire ABI ----
//
// R580 (Turing/GA100/GA10x/AD10x, `gsp_abi=580`) puts a 48-byte `GSP_MSG_QUEUE_ELEMENT` header in front of the RPC and
// page-pads the element; R620 (`gsp_abi=620`, the default, and the only ABI 620.7x firmware speaks) uses an 18-byte
// MCTP + NVDM header (`GspMsgQueueSendCommand`, `message_queue_cpu.c:483`) and is not page padded on the wire. The
// page-pads the element; R620 (`gsp_abi=620`, the default, and the only ABI 620.7x firmware speaks) uses a 24-byte
// (`0x18`) MCTP + NVDM header (`GspMsgQueueSendCommand`, `message_queue_cpu.c:483`) and is not page padded on the wire. The
// queue advances by `div_ceil(length, 4 KiB)` pages in both cases.
/// Element header bytes in front of the RPC header for `abi` (48 for R580, 24 for R620).
@ -2225,7 +2225,7 @@ mod tests {
let payload = vec![0xa5u8; 0x3a8];
let mut buf = vec![0u8; MAX_ELEMENT_BYTES];
let len = frame_element_abi(&mut buf, 72, &payload, 5, 0, GspAbiVersion::R620).unwrap();
// 18-byte MCTP+NVDM header + 32-byte RPC header + payload, not page padded.
// 24-byte (0x18) MCTP+NVDM header + 32-byte RPC header + payload, not page padded.
assert_eq!(
len,
MSG_ELEM_HDR_620_NO_ENC + RPC_HEADER_BYTES + payload.len()
@ -2276,7 +2276,11 @@ mod tests {
max_rpc_payload(GspAbiVersion::R620),
max_rpc_payload(GspAbiVersion::R580) + ELEMENT_HEADER_BYTES - MSG_ELEM_HDR_620_NO_ENC
);
// The largest payload the 620 header allows must actually frame, and fill one 64 KiB element.
let max_payload = vec![0u8; max_rpc_payload(GspAbiVersion::R620)];
let mut buf = vec![0u8; MAX_ELEMENT_BYTES];
let len = frame_element_abi(&mut buf, 76, &max_payload, 0, 0, GspAbiVersion::R620).unwrap();
assert_eq!(len, MAX_ELEMENT_BYTES);
let too_big = vec![0u8; max_rpc_payload(GspAbiVersion::R620) + 1];
assert!(frame_element_abi(&mut buf, 76, &too_big, 0, 0, GspAbiVersion::R620).is_err());
}

View File

@ -380,8 +380,10 @@ pub fn write_message(
if out.len() < total {
return Err(Error::Truncated);
}
// `black_box` keeps LLVM from turning this into a `memset` once `gmcapi` is linked into
// `nv-rust.o_binary`, where the module gate forbids `memset`.
for b in out[..HEADER_BYTES].iter_mut() {
*b = 0;
*b = core::hint::black_box(0);
}
out[0..4].copy_from_slice(&command.to_le_bytes());
out[4..8].copy_from_slice(&payload_bytes.to_le_bytes());
@ -511,7 +513,9 @@ where
0x5 => count, // NVKV_OPCODE_ARRAY64
_ => return Err(Error::Unsupported),
};
if follow > 0 {
// Branch on the opcode, not on `follow`: a zero-count `SEQ*`/`ARRAY*` entry is a real
// entry with an empty value, and `nvkvDecode` reports it rather than skipping it.
if opcode != NVKV_OPCODE_IMM32 {
let end = at.checked_add(follow).ok_or(Error::Truncated)?;
if end > words.len() {
return Err(Error::Truncated);
@ -891,6 +895,31 @@ mod tests {
assert!(decode(&[instr, 1], |_, _, _| {}).is_err());
}
#[test]
fn zero_count_sequences_are_reported_not_skipped() {
let words = [
instruction(NVKV_OPCODE_SEQ32, sc_key::INTERNAL_CLIENT_HANDLE, 0),
instruction(NVKV_OPCODE_ARRAY8, sc_key::GPU_NAME_STRING, 0),
];
let mut seen = [(0u16, 0usize); 2];
let mut n = 0usize;
let consumed = decode(&words, |key, _index, value| {
seen[n] = (
key,
match value {
NvkvValue::Seq32 { count, .. } | NvkvValue::Array8 { count, .. } => count,
_ => panic!("zero-count entry decoded as the wrong kind"),
},
);
n += 1;
})
.unwrap();
assert_eq!(consumed, 2);
assert_eq!(n, 2);
assert_eq!(seen[0], (sc_key::INTERNAL_CLIENT_HANDLE, 0));
assert_eq!(seen[1], (sc_key::GPU_NAME_STRING, 0));
}
#[test]
fn overflow_is_reported_not_hidden() {
let mut words = [0u64; 2];

View File

@ -93,6 +93,15 @@ pub const MSG_ELEM_HDR_620_NO_ENC: usize = 0x18;
pub const MSG_ELEM_HDR_620_WITH_ENC: usize = 0x28;
/// `GSP_MSG_QUEUE_ELEMENT_ENCRYPTION_OFFSET` (`+0x20`, start of encrypted region).
pub const MSG_ELEM_620_ENC_OFFSET: usize = 0x20;
/// `queueElementSizeMax` (`RM_PAGE_SIZE * 16`, `gspMsgQueueInit` in `message_queue_cpu.c`): one queue
/// element is at most 64 KiB.
pub const MSG_ELEM_MAX_BYTES: usize = 0x1_0000;
/// Largest `rpc_message_header_v.length` a plaintext 620 element carries: `gspMsgQueueGetMaxRpcSize`
/// with `GSP_MSG_QUEUE_ELEMENT_SIZE_NO_ENCRYPTION` (`0x10000 - 0x18 = 0xffe8`).
pub const RPC_MAX_LENGTH_620: usize = MSG_ELEM_MAX_BYTES - MSG_ELEM_HDR_620_NO_ENC;
/// Largest RPC an encrypted 620 element carries (`0x10000 - 0x28 = 0xffd8`): `GspMsgQueueSendCommand`
/// compares `queueElementHdrSize + size` against `queueElementSizeMax` for the header size in use.
pub const RPC_MAX_LENGTH_620_ENCRYPTED: usize = MSG_ELEM_MAX_BYTES - MSG_ELEM_HDR_620_WITH_ENC;
/// `sizeof(GMCAPI_HEADER)` (`0x28` = 40 bytes: `command`, `size`, `sequence`, `union`, `reserved[5]`).
pub const GMCAPI_HEADER_BYTES: usize = 0x28;
pub const GMCAPI_HEADER_COMMAND_ID_MASK: u32 = 0x00ff_ffff;
@ -517,7 +526,12 @@ pub fn pack_message_620(
MSG_ELEM_HDR_620_NO_ENC
};
let rpc_len = rpc.length as usize;
if !(RPC_HEADER_BYTES..=RPC_MAX_LENGTH).contains(&rpc_len) {
let rpc_max = if encrypted {
RPC_MAX_LENGTH_620_ENCRYPTED
} else {
RPC_MAX_LENGTH_620
};
if !(RPC_HEADER_BYTES..=rpc_max).contains(&rpc_len) {
return Err(Error::BadLength);
}
if rpc_len != RPC_HEADER_BYTES + payload.len() {
@ -601,7 +615,12 @@ pub fn unpack_message_620(frame: &[u8], encrypted: bool) -> Result<(u8, RpcHeade
]) as usize;
let rpc = RpcHeader::decode(&frame[hdr_bytes..])?;
let rpc_len = rpc.length as usize;
if !(RPC_HEADER_BYTES..=RPC_MAX_LENGTH).contains(&rpc_len) || rpc_len > nvdm_size {
let rpc_max = if encrypted {
RPC_MAX_LENGTH_620_ENCRYPTED
} else {
RPC_MAX_LENGTH_620
};
if !(RPC_HEADER_BYTES..=rpc_max).contains(&rpc_len) || rpc_len > nvdm_size {
return Err(Error::BadLength);
}
let total = hdr_bytes.checked_add(rpc_len).ok_or(Error::BadLength)?;
@ -1931,6 +1950,31 @@ mod tests {
assert_eq!(gmc_rsp.command_id(), gmcapi_cmd::GSP_INIT);
}
#[test]
fn gsp_620_rpc_limit_fills_one_64k_element() {
// `gspMsgQueueGetMaxRpcSize`: 0x10000 - 0x18 plaintext (0x10000 - 0x28 encrypted).
assert_eq!(RPC_MAX_LENGTH_620, 0xffe8);
assert_eq!(RPC_MAX_LENGTH_620_ENCRYPTED, 0xffd8);
let payload_len = RPC_MAX_LENGTH_620 - RPC_HEADER_BYTES;
let body = [0x3cu8; RPC_MAX_LENGTH_620 - RPC_HEADER_BYTES];
let rpc = RpcHeader::request(func::GSP_RM_CONTROL, payload_len as u32, 1, 0);
let mut frame = [0u8; MSG_ELEM_MAX_BYTES];
let (total, elems) =
pack_message_620(&mut frame, NVDM_TYPE_RM_RPC, &rpc, &body, false, None).unwrap();
assert_eq!(total, MSG_ELEM_MAX_BYTES);
assert_eq!(elems, 16);
assert!(unpack_message_620(&frame, false).is_ok());
let over = RpcHeader::request(func::GSP_RM_CONTROL, (payload_len + 1) as u32, 1, 0);
let body = [0x3cu8; RPC_MAX_LENGTH_620 - RPC_HEADER_BYTES + 1];
let mut frame = [0u8; MSG_ELEM_MAX_BYTES + 1];
assert_eq!(
pack_message_620(&mut frame, NVDM_TYPE_RM_RPC, &over, &body, false, None),
Err(Error::BadLength)
);
}
#[test]
fn unlock_registry_packs_dynbar1_p2p_unpriv_prof_and_ecc() {
let mut buf = [0u8; 512];

View File

@ -28,6 +28,8 @@ class GspOracleTests(unittest.TestCase):
"src/nvidia/inc/kernel/gsp/gsp_fw_wpr_meta.h",
"src/nvidia/interface/vgpu/rpc_global_enums.h",
"src/nvidia/interface/gmcapi/gmcapi_base.h",
"src/nvidia/interface/gmcapi/gmcapi_system_info.h",
"src/nvidia/interface/gmcapi/gmcapi_gsp_config.h",
]
def write_tree(self):
@ -78,16 +80,27 @@ class GspOracleTests(unittest.TestCase):
self.assertEqual(result["header_sha256"], expected)
self.assertEqual(result["diffs"][0]["field"], "sizeof_gmcapi_header")
def test_wrong_release_cannot_pass_the_620_70_06_gate(self):
def test_both_620_patch_releases_pass_the_gate(self):
self.write_tree()
for version in ("620.70.06", "620.73.04"):
(self.root / "version.mk").write_text(
f"NVIDIA_VERSION = {version}\n", encoding="utf-8"
)
rc, result = self.run_oracle(oracle.EXPECTED_RUST)
self.assertEqual(rc, 0, result)
self.assertEqual(result["status"], "passed")
self.assertEqual(result["source_version"], version)
def test_unsupported_release_is_skipped(self):
self.write_tree()
(self.root / "version.mk").write_text(
"NVIDIA_VERSION = 620.73.04\n", encoding="utf-8"
"NVIDIA_VERSION = 619.99.99\n", encoding="utf-8"
)
rc, result = self.run_oracle(oracle.EXPECTED_RUST)
self.assertEqual(rc, 2)
self.assertEqual(result["status"], "skipped")
self.assertEqual(result["source_version"], "620.73.04")
self.assertIn("expected NVIDIA_VERSION 620.70.06", result["error"])
self.assertEqual(result["source_version"], "619.99.99")
self.assertIn("expected NVIDIA_VERSION 620.70.06 or 620.73.04", result["error"])
if __name__ == "__main__":

View File

@ -153,8 +153,6 @@ int main(void) {
printf(" \"rpc_num_functions\": %u,\n", (unsigned)NV_VGPU_MSG_FUNCTION_NUM_FUNCTIONS);
printf(" \"rpc_num_events\": %u,\n", (unsigned)NV_VGPU_MSG_EVENT_NUM_EVENTS);
printf(" \"rpc_bind_bar2\": %u,\n", (unsigned)NV_VGPU_MSG_EVENT_BIND_BAR2);
printf(" \"gmcapi_id_mask\": %u,\n", (unsigned)GMCAPI_HEADER_COMMAND_ID_MASK);
printf(" \"gmcapi_flags_response\": %u,\n", (unsigned)GMCAPI_HEADER_COMMAND_FLAGS_RESPONSE);
printf(" \"rpc_gsp_static_info\": %u,\n", (unsigned)NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO);
printf(" \"rpc_gsp_set_system_info\": %u,\n", (unsigned)NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO);
printf(" \"rpc_set_registry\": %u,\n", (unsigned)NV_VGPU_MSG_FUNCTION_SET_REGISTRY);
@ -268,8 +266,6 @@ EXPECTED_RUST = {
"rpc_num_events": 0x102E,
"rpc_bind_bar2": 0x1024,
"rpc_send_guest_stun": 232,
"gmcapi_id_mask": 0x00FF_FFFF,
"gmcapi_flags_response": 0x0100_0000,
"rpc_gsp_static_info": 0x41,
"rpc_gsp_set_system_info": 0x48,
"rpc_set_registry": 0x49,