Skip to contentSkip to conversation

s390-ccw: print carriage return with new lines

PATCH In review Christian Borntraeger 7 participants 15 messages Oct 20, 2017 – Oct 25, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390-ccw: print carriage return with new lines

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +3 -0
index 25d4d21..a8bd204 100644
@@ -33,6 +33,9 @@ typedef unsigned long long __u64;
 #ifndef EBUSY
 #define EBUSY   2
 #endif
+#ifndef EFBIG
+#define EFBIG   3
+#endif
 #ifndef NULL
 #define NULL    0
 #endif
pc-bios/s390-ccw/sclp.c +14 -3
index b1fc8ff..4795259 100644
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;
     sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-    sccb->ebh.length = sizeof(EventBufferHeader) + len;
+    sccb->ebh.length = sizeof(EventBufferHeader) + data_len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 
Signature · 1 line
2.9.4
#1 lore raw
Quoted 62 lines · Christian Borntraeger

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +3 -0
index 25d4d21..a8bd204 100644
@@ -33,6 +33,9 @@ typedef unsigned long long __u64;
 #ifndef EBUSY
 #define EBUSY   2
 #endif
+#ifndef EFBIG
+#define EFBIG   3
+#endif
 #ifndef NULL
 #define NULL    0
 #endif
pc-bios/s390-ccw/sclp.c +3 -1
index b1fc8ff..4795259 100644
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 96

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Alex

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +1 -2
     sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-    sccb->ebh.length = sizeof(EventBufferHeader) + len;
+    sccb->ebh.length = sizeof(EventBufferHeader) + data_len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 
#2 lore raw
Quoted 80 lines · Alexander Graf
Quoted 62 lines · Christian Borntraeger

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +3 -0
index 25d4d21..a8bd204 100644
@@ -33,6 +33,9 @@ typedef unsigned long long __u64;
 #ifndef EBUSY
 #define EBUSY   2
 #endif
+#ifndef EFBIG
+#define EFBIG   3
+#endif
 #ifndef NULL
 #define NULL    0
 #endif
pc-bios/s390-ccw/sclp.c +3 -1
index b1fc8ff..4795259 100644
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 96

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

Quoted 2 lines

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Yes, well spotted.
The write function is used in other code (SLOF related network boot), so we should change it to respect the length, I think.

Quoted 15 lines

Alex

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +1 -2
     sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-    sccb->ebh.length = sizeof(EventBufferHeader) + len;
+    sccb->ebh.length = sizeof(EventBufferHeader) + data_len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 
#3 lore raw

On 10/20/2017 12:41 PM, Christian Borntraeger wrote:
[...]

Quoted 28 lines
Quoted 28 lines
Quoted 13 lines
(quoted diff) +3 -1
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

Review static int _strlen(const char *str) · line 96

Quoted 10 lines
(quoted diff) +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

Quoted 6 lines
Quoted 2 lines

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Yes, well spotted.
The write function is used in other code (SLOF related network boot), so we should change it to respect the length, I think.

Something like this on top?

pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {
         if (data_len > SCCB_DATA_LEN - 1) {
             return -EFBIG;
         }
#4 lore raw
Quoted 32 lines · Christian Borntraeger

On 10/20/2017 12:41 PM, Christian Borntraeger wrote:
[...]

Quoted 28 lines
Quoted 28 lines
Quoted 13 lines
(quoted diff) +3 -1
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

Review static int _strlen(const char *str) · line 96

Quoted 10 lines
(quoted diff) +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Yes, well spotted.
The write function is used in other code (SLOF related network boot), so we should change it to respect the length, I think.

Something like this on top?

I think that basically gets you back to the original semantics. I'm not terribly thrilled about the readability of the function though, but that's your call :)

Alex

Quoted 13 lines
pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {
         if (data_len > SCCB_DATA_LEN - 1) {
             return -EFBIG;
         }
#5 lore raw
Quoted 42 lines · Alexander Graf
Quoted 39 lines · Christian Borntraeger

On 10/20/2017 12:41 PM, Christian Borntraeger wrote:
[...]

Quoted 34 lines
Quoted 28 lines
Quoted 13 lines
(quoted diff) +3 -1
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

Review static int _strlen(const char *str) · line 96

Quoted 10 lines
(quoted diff) +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

Quoted 2 lines

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Yes, well spotted.
The write function is used in other code (SLOF related network boot), so we should change it to respect the length, I think.

Quoted 6 lines
Quoted 2 lines

Something like this on top?

I think that basically gets you back to the original semantics. I'm not terribly thrilled about the readability of the function though, but that's your call :)

In the end I want to refactor the whole thing. we have write and sclp_print. So there is certainly room for improvement. With softfreeze approaching this seems like the minimal fix. I will respin if Conny is ok with this approach.

Quoted 18 lines

Alex

Quoted 13 lines
pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {
         if (data_len > SCCB_DATA_LEN - 1) {
             return -EFBIG;
         }
#6 lore raw
In reply to Christian Borntraeger
Quoted 44 lines · Christian Borntraeger

On 10/20/2017 12:41 PM, Christian Borntraeger wrote:
[...]

Quoted 28 lines
Quoted 28 lines
Quoted 13 lines
(quoted diff) +3 -1
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {

Review static int _strlen(const char *str) · line 96

Quoted 10 lines
(quoted diff) +10 -0
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+
+    sccb->h.length = sizeof(WriteEventData) + data_len;

This subtly changes the semantics of the write() function from an explicitly passed in "len" argument to NULL termination determined sizing, no?

Quoted 6 lines
Quoted 2 lines

In that case, wouldn't it make sense to either remove the len argument altogether or keep respecting it?

Yes, well spotted.
The write function is used in other code (SLOF related network boot), so we should change it to respect the length, I think.

Something like this on top?

pc-bios/s390-ccw/sclp.c long write(int fd, const void *str, size_t len) · line 86

Quoted 5 lines
pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {

I'd maybe rather use "len > 0" as end condition, but that's just cosmetics. Anyway, patch looks fine to me with one of these changes. And for what it's worth, SLOF is doing a similar '\n' -> '\r\n' convertion in its write function (see e.g.
https://github.com/aik/SLOF/blob/master/llfw/clib/iolib.c), so I think this is the right way to go here, too.

Thomas

#7 lore raw
In reply to Christian Borntraeger
Quoted 6 lines · Christian Borntraeger

pc-bios/s390-ccw/sclp.c long write(int fd, const void *str, size_t len) · line 89

Quoted 8 lines
pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {
         if (data_len > SCCB_DATA_LEN - 1) {
             return -EFBIG;
         }

The write function returns len, wouldn't this change make write return 0?

#8 lore raw

Collin,

can you take care of the comments and send out a new version?

Quoted 18 lines · Farhan Ali
Quoted 6 lines · Christian Borntraeger

pc-bios/s390-ccw/sclp.c long write(int fd, const void *str, size_t len) · line 89

Quoted 8 lines
pc-bios/s390-ccw/sclp.c +1 -1
@@ -83,7 +83,7 @@ long write(int fd, const void *str, size_t len)
         return -EIO;
     }
 
-    for (p = str; *p; ++p) {
+    for (p = str; len ; ++p, len--) {
         if (data_len > SCCB_DATA_LEN - 1) {
             return -EFBIG;
         }

The write function returns len, wouldn't this change make write return 0?

#9 lore raw
In reply to Christian Borntraeger
Quoted 24 lines · On Fri, 20 Oct 2017 12:25:17 +0200 Christian Borntraeger…

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
Quoted 6 lines
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

I'd defer that to a second pullreq before softfreeze where we can collect the stragglers (currently building a pullreq). And I'd also like to delegate that second pullreq to you, as I'll be busy/offline after KVM Forum.

#10 lore raw
Quoted 34 lines · Cornelia Huck
Quoted 24 lines · On Fri, 20 Oct 2017 12:25:17 +0200 Christian Borntraeger…

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
Quoted 6 lines
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
Quoted 4 lines

I'd defer that to a second pullreq before softfreeze where we can collect the stragglers (currently building a pullreq). And I'd also like to delegate that second pullreq to you, as I'll be busy/offline after KVM Forum.

Sure I can do that in a 2nd pull req after KVM forum.

#11 lore raw
Quoted 35 lines · On Fri, 20 Oct 2017 14:27:12 +0200 Christian Borntraeger…
Quoted 34 lines · Cornelia Huck
Quoted 30 lines · On Fri, 20 Oct 2017 12:25:17 +0200 Christian Borntraeger…

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)  
Quoted 7 lines
Quoted 4 lines

I'd defer that to a second pullreq before softfreeze where we can collect the stragglers (currently building a pullreq). And I'd also like to delegate that second pullreq to you, as I'll be busy/offline after KVM Forum.

Sure I can do that in a 2nd pull req after KVM forum.

Cool, thx.

#12 lore raw
In reply to Christian Borntraeger
Quoted 15 lines · Christian Borntraeger

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

Quoted 9 lines

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                          Using MAC address: 02:01:02:03:04:05
                                                                Requesting information via DHCP:  010

The check for buffer overflow was not there previously, or?

Maybe integrate that in the commit message too.

Quoted 31 lines
---
 pc-bios/s390-ccw/s390-ccw.h |  3 +++
 pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +3 -0
index 25d4d21..a8bd204 100644
@@ -33,6 +33,9 @@ typedef unsigned long long __u64;
 #ifndef EBUSY
 #define EBUSY   2
 #endif
+#ifndef EFBIG
+#define EFBIG   3
+#endif
 #ifndef NULL
 #define NULL    0
 #endif
pc-bios/s390-ccw/sclp.c +2 -0
index b1fc8ff..4795259 100644
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
 long write(int fd, const void *str, size_t len)
 {
     WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 89

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +4 -1
 
     if (fd != 1 && fd != 2) {
         return -EIO;
     }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }

We could also do a partial write or do more that one sclp_service_call calls.

I don't think EFBIG is entirely correct here. From the man page:
"""
EFBIG An attempt was made to write a file that exceeds the implementa-
tion-defined maximum file size or the process’s file size limit, or to write at a position past the maximum allowed offset.
"""

That's not what we have here IMHO.

Quoted 6 lines
pc-bios/s390-ccw/sclp.c +6 -0
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 103

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +2 -2
+    sccb->h.length = sizeof(WriteEventData) + data_len;
     sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-    sccb->ebh.length = sizeof(EventBufferHeader) + len;
+    sccb->ebh.length = sizeof(EventBufferHeader) + data_len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 

Otherwise LGTM

Halil

#13 lore raw
Quoted 26 lines · Halil Pasic
Quoted 15 lines · Christian Borntraeger

From: "Collin L. Walling" <[email protected]>

The sclp console in the s390 bios writes raw data, leading console emulators (such as virsh console) to treat a new line ('\n') as just a new line instead of as a Unix line feed. Because of this, output appears in a "stair case" pattern.

Let's print \r\n on every occurrence of a new line in the string passed to write to amend this issue.

This is in sync with the guest Linux code in drivers/s390/char/sclp_vt220.c which also does a line feed conversion in the console part of the driver.

Quoted 9 lines

This fixes the s390-ccw and s390-netboot output like

$ virsh start test --console
Domain test started
Connected to domain test
Escape character is ^]
Network boot starting...
                           Using MAC address: 02:01:02:03:04:05
                                                                 Requesting information via DHCP:  010
Quoted 3 lines

The check for buffer overflow was not there previously, or?

Maybe integrate that in the commit message too.

Good point.

Quoted 45 lines
Quoted 32 lines
---
  pc-bios/s390-ccw/s390-ccw.h |  3 +++
  pc-bios/s390-ccw/sclp.c     | 17 ++++++++++++++---
  2 files changed, 17 insertions(+), 3 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +3 -0
index 25d4d21..a8bd204 100644
@@ -33,6 +33,9 @@ typedef unsigned long long __u64;
  #ifndef EBUSY
  #define EBUSY   2
  #endif
+#ifndef EFBIG
+#define EFBIG   3
+#endif
  #ifndef NULL
  #define NULL    0
  #endif
pc-bios/s390-ccw/sclp.c +2 -0
index b1fc8ff..4795259 100644
@@ -76,17 +76,28 @@ static int _strlen(const char *str)
  long write(int fd, const void *str, size_t len)
  {
      WriteEventData *sccb = (void *)_sccb;
+    const char *p;
+    size_t data_len = 0;

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 89

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +4 -1
 
      if (fd != 1 && fd != 2) {
          return -EIO;
      }
 
-    sccb->h.length = sizeof(WriteEventData) + len;
+    for (p = str; *p; ++p) {
+        if (data_len > SCCB_DATA_LEN - 1) {
+            return -EFBIG;
+        }

We could also do a partial write or do more that one sclp_service_call calls.

Quoted 8 lines

I don't think EFBIG is entirely correct here. From the man page:
"""
EFBIG An attempt was made to write a file that exceeds the implementa-
tion-defined maximum file size or the process’s file size limit, or to write at a position past the maximum allowed offset.
"""

That's not what we have here IMHO.

From my perspective, the error code was a tie between EFBIG (consider max sccb size as the maximum allowed offset) and ENOSPC:

"""
ENOSPC The device containing the file referred to by /fd/has no room for the data.
"""

(consider "the file" as the sccb data buffer)

However, I extremely doubt we'll ever encounter an overflow from printing during the bios (why would we print something that large?) ... perhaps the check is redundant?

Quoted 22 lines
Quoted 7 lines
pc-bios/s390-ccw/sclp.c +6 -0
+        if (*p == '\n') {
+            sccb->data[data_len++] = '\r';
+        }
+        sccb->data[data_len++] = *p;
+    }
+

pc-bios/s390-ccw/sclp.c static int _strlen(const char *str) · line 103

Quoted 10 lines
pc-bios/s390-ccw/sclp.c +2 -2
+    sccb->h.length = sizeof(WriteEventData) + data_len;
      sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-    sccb->ebh.length = sizeof(EventBufferHeader) + len;
+    sccb->ebh.length = sizeof(EventBufferHeader) + data_len;
      sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
      sccb->ebh.flags = 0;
-    memcpy(sccb->data, str, len);
 
      sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 

Otherwise LGTM

Halil

Signature · 1 line
- Collin L Walling
#14 lore raw
Quoted 18 lines · Collin L. Walling
Quoted 8 lines
Quoted 5 lines

-    sccb->h.length = sizeof(WriteEventData) + len; +    for (p = str; *p; ++p) {
+        if (data_len > SCCB_DATA_LEN - 1) { +            return -EFBIG;
+        }

We could also do a partial write or do more that one sclp_service_call calls.

Quoted 8 lines

I don't think EFBIG is entirely correct here. From the man page:
"""
        EFBIG  An attempt was made to write a file that exceeds the implementa- tion-defined maximum file size or the process’s file size limit, or to write at a position past the maximum allowed offset. """

That's not what we have here IMHO.

Quoted 9 lines

From my perspective, the error code was a tie between EFBIG (consider max sccb size as the maximum allowed offset) and ENOSPC:

"""
ENOSPC The device containing the file referred to by /fd/has no room for the data.
"""

From where I stand, the file behind the fd is the VT220 terminal you talk to via sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb) -- you can check the AR).

In the end, the target scope (in sense of expected client code) of this function very limited, and AFAIU does not necessarily care about error codes (see sclp_print). So basically anything is fine with me, because it does not really matter -- but just because it does not really matter.

Halil

Quoted 6 lines

(consider "the file" as the sccb data buffer)

However, I extremely doubt we'll ever encounter an overflow from printing during the bios (why would we print something that large?) ... perhaps the check is redundant?