Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions driver/patches/sec2-postbl-plm-ss-cfg.patch
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/

{
// If the new FB layout requires a scrubber ucode to scrub additional space, prepare it now
@@ -4821,6 +4857,143 @@
@@ -4821,6 +4857,147 @@
// Setup arguments for bootstrapping GSP
NV_CHECK_OK_OR_RETURN(LEVEL_ERROR, kgspPrepareForBootstrap_HAL(pGpu, pKernelGsp, KGSP_BOOT_MODE_NORMAL));

Expand Down Expand Up @@ -123,9 +123,13 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
+ opened = NV_TRUE;
+ }
+ if (!opened)
+ {
+ NV_PRINTF(LEVEL_ERROR,
+ "SEC2_DEBUG: FAILED to open %s after 2 attempts\n",
+ plmTable[plmIdx].name);
+ if (plmIdx < 4)
+ return NV_ERR_INVALID_STATE;
+ }
+ }
+
+ GPU_REG_WR32(pGpu, 0x001fa824U, wpr2Lo);
Expand Down Expand Up @@ -210,7 +214,13 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
// Release the API lock if relaxed locking for parallel init is enabled
NvBool bRelaxedLocking = _kgspShouldRelaxGspInitLocking(pGpu);
if (bRelaxedLocking)
@@ -5662,6 +5816,84 @@
@@ -4842,3 +5019,4 @@
- if (status != NV_OK && !pGpu->pGpuArch->bGpuArchIsZeroFb)
+ if (status != NV_OK && !pGpu->pGpuArch->bGpuArchIsZeroFb &&
+ !_kgspSec2PostblTimingEnabled(pGpu))
{
// Increment the bootAttempt counter only on failure to boot GSP
@@ -5662,6 +5840,88 @@
pKernelGsp->gspRmBootUcodeSize = 0;
}

Expand All @@ -229,7 +239,10 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
+{
+ NvU64 i;
+
+ for (i = 0; i + sizeof(NvU32) <= signatureSize; i += sizeof(NvU32))
+ NV_ASSERT_OR_RETURN_VOID(pSignatureVa != NULL &&
+ signatureSize >= SEC2_POSTBL_TIMING_SIGNATURE_SIZE);
+
+ for (i = 0; i + sizeof(NvU32) <= SEC2_POSTBL_TIMING_SIGNATURE_SIZE; i += sizeof(NvU32))
+ _kgspSec2PostblTimingPutU32(pSignatureVa, (NvU32)i, SEC2_POSTBL_TIMING_FILL_DWORD);
+
+ _kgspSec2PostblTimingPutU32(pSignatureVa, 0x1100, 0x00000007U);
Expand Down Expand Up @@ -265,7 +278,8 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
+{
+ NvU8 *pSignatureVa;
+
+ if (pKernelGsp->pSignatureMemdesc == NULL)
+ if (pKernelGsp->pSignatureMemdesc == NULL ||
+ memdescGetSize(pKernelGsp->pSignatureMemdesc) < SEC2_POSTBL_TIMING_SIGNATURE_SIZE)
+ return NV_ERR_INVALID_STATE;
+
+ pSignatureVa = memdescMapInternal(pGpu, pKernelGsp->pSignatureMemdesc, TRANSFER_FLAGS_NONE);
Expand Down Expand Up @@ -295,7 +309,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
static NV_STATUS
_kgspCreateSignatureMemdesc
(
@@ -5672,6 +5904,9 @@
@@ -5672,6 +5932,9 @@
{
NV_STATUS status = NV_OK;
NvU8 *pSignatureVa = NULL;
Expand All @@ -305,7 +319,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
NvU64 flags = MEMDESC_FLAGS_NONE;

if (confComputeForceUnprotAlloc(pGpu))
@@ -5682,7 +5917,7 @@
@@ -5682,7 +5945,7 @@
// NOTE: align to 256 because that's the alignment needed for Booter DMA
NV_CHECK_OK_OR_RETURN(LEVEL_ERROR,
memdescCreate(&pKernelGsp->pSignatureMemdesc, pGpu,
Expand All @@ -314,7 +328,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
NV_TRUE, ADDR_SYSMEM, NV_MEMORY_CACHED, flags));

memdescTagAlloc(status,
@@ -5694,8 +5929,48 @@
@@ -5694,8 +5957,48 @@
(pSignatureVa != NULL) ? NV_OK : NV_ERR_INSUFFICIENT_RESOURCES,
fail_alloc);

Expand Down Expand Up @@ -365,7 +379,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/

memdescUnmapInternal(pGpu, pKernelGsp->pSignatureMemdesc, 0);
pSignatureVa = NULL;
@@ -5709,6 +5984,69 @@
@@ -5709,6 +6012,69 @@
memdescDestroy(pKernelGsp->pSignatureMemdesc);
pKernelGsp->pSignatureMemdesc = NULL;

Expand Down Expand Up @@ -435,7 +449,7 @@ diff -Naur a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/
return status;
}

@@ -6612,6 +6950,51 @@
@@ -6612,6 +6978,51 @@
{
GspStaticConfigInfo *pGSCI = GPU_GET_GSP_STATIC_INFO(pGpu);

Expand Down
129 changes: 129 additions & 0 deletions tests/test_payload.py

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't be needing the test, please remove that.

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import hashlib
import re
import shutil
import subprocess

import repo


def payload_source():
patch = (repo.ROOT / "driver/patches/sec2-postbl-plm-ss-cfg.patch").read_text()
return "\n".join(line[1:] for line in patch.splitlines()
if line.startswith("+") and not line.startswith("+++"))


def function(source, name):
start = re.search(r"(?:static void|NV_STATUS)\n" + name + r"\(", source).start()
end = source.index("\n}", start) + 2
return source[start:end]


def test_payload_bounds(tmp_path):
assert shutil.which("cc"), "a C compiler is required"
source = payload_source()
constants = "\n".join(re.findall(r"^#define SEC2_POSTBL_TIMING_(?:SIGNATURE_SIZE|FILL_DWORD)\s+.*$",
source, re.M))
functions = "\n".join(function(source, name) for name in (
"_kgspSec2PostblTimingPutU32", "_kgspSec2PostblTimingFillPayload",
"kgspSec2PostblTimingRefillPayload"))
code = r'''
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint8_t NvU8;
typedef uint32_t NvU32;
typedef uint64_t NvU64;
typedef int NV_STATUS;
typedef struct { int unused; } OBJGPU;
typedef struct { NvU64 size; NvU8 *data; } MEMORY_DESCRIPTOR;
typedef struct { NvU64 sysmemAddrOfSignature, sizeOfSignature; } WPR_META;
typedef struct {
MEMORY_DESCRIPTOR *pSignatureMemdesc, *metaDesc;
WPR_META *meta;
} KernelGsp;
#define NV_OK 0
#define NV_ERR_INVALID_STATE 1
#define NV_ERR_INSUFFICIENT_RESOURCES 2
#define TRANSFER_FLAGS_NONE 0
#define AT_GPU 0
#define NV_ASSERT_OR_RETURN_VOID(x) do { if (!(x)) return; } while (0)
#define SEC2_POSTBL_TIMING_WPR_META(k) ((k)->meta)
#define SEC2_POSTBL_TIMING_WPR_META_DESC(k) ((k)->metaDesc)
#define memdescGetSize(d) ((d)->size)
static unsigned maps, unmaps, flushes;
static NvU8 *memdescMapInternal(OBJGPU *g, MEMORY_DESCRIPTOR *d, int flags)
{ (void)g; (void)flags; maps++; return d->data; }
static void memdescUnmapInternal(OBJGPU *g, MEMORY_DESCRIPTOR *d, int flags)
{ (void)g; (void)d; (void)flags; unmaps++; }
static void memdescFlushCpuCaches(OBJGPU *g, MEMORY_DESCRIPTOR *d)
{ (void)g; (void)d; flushes++; }
static NvU64 memdescGetPhysAddr(MEMORY_DESCRIPTOR *d, int target, int offset)
{ (void)d; (void)target; (void)offset; return 0xabc000; }
'''
main = r'''
static void unchanged(const NvU8 *data, size_t size)
{
for (size_t i = 0; i < size; i++) assert(data[i] == 0xa5);
}
int main(void)
{
const size_t sizes[] = {0, 1, 4096, SEC2_POSTBL_TIMING_SIGNATURE_SIZE - 1};
OBJGPU gpu = {0};
KernelGsp gsp = {0};
assert(kgspSec2PostblTimingRefillPayload(&gpu, &gsp, 0, 0) == NV_ERR_INVALID_STATE);
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
{
size_t allocated = sizes[i] ? sizes[i] : 1;
NvU8 *data = malloc(allocated);
assert(data);
memset(data, 0xa5, allocated);
_kgspSec2PostblTimingFillPayload(data, sizes[i], 0x9a0148, 0xffffffff);
unchanged(data, allocated);
MEMORY_DESCRIPTOR desc = {sizes[i], data};
gsp.pSignatureMemdesc = &desc;
assert(kgspSec2PostblTimingRefillPayload(&gpu, &gsp, 0, 0) == NV_ERR_INVALID_STATE);
unchanged(data, allocated);
free(data);
}
assert(maps == 0 && unmaps == 0 && flushes == 0);
_kgspSec2PostblTimingFillPayload(NULL, SEC2_POSTBL_TIMING_SIGNATURE_SIZE, 0, 0);
MEMORY_DESCRIPTOR desc = {SEC2_POSTBL_TIMING_SIGNATURE_SIZE, NULL};
gsp.pSignatureMemdesc = &desc;
assert(kgspSec2PostblTimingRefillPayload(&gpu, &gsp, 0, 0) == NV_ERR_INSUFFICIENT_RESOURCES);
assert(maps == 1 && unmaps == 0 && flushes == 0);

const size_t size = SEC2_POSTBL_TIMING_SIGNATURE_SIZE;
NvU8 *data = malloc(size);
NvU8 *larger = malloc(size + 32);
assert(data && larger);
desc.data = data;
WPR_META meta = {0};
MEMORY_DESCRIPTOR metaDesc = {sizeof(meta), (NvU8 *)&meta};
gsp.meta = &meta;
gsp.metaDesc = &metaDesc;
assert(kgspSec2PostblTimingRefillPayload(&gpu, &gsp, 0x9a0148, 0xffffffff) == NV_OK);
assert(maps == 2 && unmaps == 1 && flushes == 2);
assert(meta.sysmemAddrOfSignature == 0xabc000 && meta.sizeOfSignature == size);
assert(fwrite(data, 1, size, stdout) == size);
memset(larger, 0xa5, size + 32);
_kgspSec2PostblTimingFillPayload(larger, size + 32, 0x9a0148, 0xffffffff);
assert(memcmp(data, larger, size) == 0);
unchanged(larger + size, 32);
_kgspSec2PostblTimingFillPayload(data, size, 0x1fa7cc, 0xfffff0ff);
assert(fwrite(data, 1, size, stdout) == size);
free(larger);
free(data);
return 0;
}
'''
path = tmp_path / "payload.c"
path.write_text(code + constants + "\n" + functions + main)
binary = tmp_path / "payload"
subprocess.run(["cc", "-std=c99", "-O1", "-g", "-Wall", "-Wextra", "-Werror",
"-fsanitize=address,undefined", "-fno-omit-frame-pointer",
str(path), "-o", str(binary)], check=True, capture_output=True)
result = subprocess.run([str(binary)], capture_output=True)
assert result.returncode == 0, result.stderr.decode()
assert hashlib.sha256(result.stdout).hexdigest() == "5ddec711ac9c690c48a0ee67e199d871a3b4976f1ec652df85afff93cbbdb319"