diff --git a/gdb/NEWS b/gdb/NEWS
index e46a5108272..967787783e2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -36,6 +36,13 @@
   subsequent runs of the inferior will use the same arguments as the
   first run.
 
+* GDB now supports arbitrary baud rates on systems where the host libc
+  accepts them via cfsetispeed/cfsetospeed (such as glibc 2.42+).
+  On Linux systems where this is not available, GDB falls back to the
+  termios2 (BOTHER) interface.  The legacy termios fallback is now
+  only compiled on systems where struct termios exposes c_ispeed
+  and c_ospeed.
+
 * Support for stabs debug information has been removed.
 
 * Support for mdebug debug information has been removed.
diff --git a/gdb/config.in b/gdb/config.in
index b11fcf18372..b7aacab898a 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -110,6 +110,9 @@
    the CoreFoundation framework. */
 #undef HAVE_CFPREFERENCESCOPYAPPVALUE
 
+/* Define if cfsetispeed/cfsetospeed accept arbitrary baud rates */
+#undef HAVE_CFSETSPEED_ARBITRARY
+
 /* Define to 1 if you have the `clearenv' function. */
 #undef HAVE_CLEARENV
 
@@ -517,6 +520,9 @@
 /* Define to 1 if `st_blocks' is a member of `struct stat'. */
 #undef HAVE_STRUCT_STAT_ST_BLOCKS
 
+/* Define to 1 if `c_ospeed' is a member of `struct termios'. */
+#undef HAVE_STRUCT_TERMIOS_C_OSPEED
+
 /* Define to 1 if `td_pcb' is a member of `struct thread'. */
 #undef HAVE_STRUCT_THREAD_TD_PCB
 
diff --git a/gdb/configure b/gdb/configure
index 12c54521682..ef2ee9f72ec 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -27728,6 +27728,58 @@ if test "$ac_res" != no; then :
 fi
 
 
+# Check whether cfsetispeed/cfsetospeed accept arbitrary baud rates.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether cfsetispeed/cfsetospeed accept arbitrary baud rates" >&5
+$as_echo_n "checking whether cfsetispeed/cfsetospeed accept arbitrary baud rates... " >&6; }
+if ${gdb_cv_cfsetspeed_arbitrary+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <termios.h>
+int
+main ()
+{
+
+       #if B9600 != 9600
+       #error B-constants are not numeric symbols
+       #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  gdb_cv_cfsetspeed_arbitrary=yes
+else
+  gdb_cv_cfsetspeed_arbitrary=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_cfsetspeed_arbitrary" >&5
+$as_echo "$gdb_cv_cfsetspeed_arbitrary" >&6; }
+
+if test "$gdb_cv_cfsetspeed_arbitrary" = yes; then
+
+$as_echo "#define HAVE_CFSETSPEED_ARBITRARY 1" >>confdefs.h
+
+fi
+
+# Check for members required by the legacy Linux custom baud rate path.
+ac_fn_c_check_member "$LINENO" "struct termios" "c_ospeed" "ac_cv_member_struct_termios_c_ospeed" "#include <termios.h>
+"
+if test "x$ac_cv_member_struct_termios_c_ospeed" = xyes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_TERMIOS_C_OSPEED 1
+_ACEOF
+
+
+fi
+
+
 
 
 # Check whether --with-jit-reader-dir was given.
diff --git a/gdb/configure.ac b/gdb/configure.ac
index cf8078e1d89..e558cf44703 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -725,6 +725,28 @@ AC_CONFIG_FILES([jit-reader.h:jit-reader.in])
 
 AC_SEARCH_LIBS(dlopen, dl)
 
+# Check whether cfsetispeed/cfsetospeed accept arbitrary baud rates.
+AC_CACHE_CHECK([whether cfsetispeed/cfsetospeed accept arbitrary baud rates],
+  [gdb_cv_cfsetspeed_arbitrary], [
+  AC_COMPILE_IFELSE(
+    [AC_LANG_PROGRAM([[#include <termios.h>]],
+      [[
+       #if B9600 != 9600
+       #error B-constants are not numeric symbols
+       #endif
+      ]])],
+    [gdb_cv_cfsetspeed_arbitrary=yes],
+    [gdb_cv_cfsetspeed_arbitrary=no])
+])
+
+if test "$gdb_cv_cfsetspeed_arbitrary" = yes; then
+  AC_DEFINE([HAVE_CFSETSPEED_ARBITRARY], [1],
+	    [Define if cfsetispeed/cfsetospeed accept arbitrary baud rates])
+fi
+
+# Check for members required by the legacy Linux custom baud rate path.
+AC_CHECK_MEMBERS([struct termios.c_ospeed], [], [], [[#include <termios.h>]])
+
 GDB_AC_WITH_DIR([JIT_READER_DIR], [jit-reader-dir],
 		[directory to load the JIT readers from],
 		[${libdir}/gdb])
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index c295a9c5ba1..0a6cdf0f630 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -55,7 +55,10 @@
 
 #include "gdbsupport/scoped_ignore_sigttou.h"
 
-#if defined(HAVE_SYS_IOCTL_H) && (defined(BOTHER) || defined(IOSSIOSPEED))
+#if defined(HAVE_CFSETSPEED_ARBITRARY) || \
+    (defined(HAVE_SYS_IOCTL_H) && \
+     ((defined(BOTHER) && defined(HAVE_STRUCT_TERMIOS_C_OSPEED)) || \
+      defined(IOSSIOSPEED)))
 #  define HAVE_CUSTOM_BAUDRATE_SUPPORT 1
 #endif
 
@@ -508,6 +511,33 @@ set_baudcode_baudrate (struct serial *scb, int baud_code)
     perror_with_name (_("could not set tty state"));
 }
 
+#if defined(HAVE_CFSETSPEED_ARBITRARY)
+
+/* Set a custom baud rate using the POSIX cfsetispeed/cfsetospeed
+   interface.  Supported in glibc 2.42+ and expected to be
+   standardized in a future POSIX revision.  It is platform-agnostic
+   and also covers systems like GNU Hurd that do not provide BOTHER.  */
+
+static void
+set_custom_baudrate_posix (int fd, int rate)
+{
+  struct termios tio;
+
+  if (tcgetattr (fd, &tio) < 0)
+    perror_with_name (_("Can not get current baud rate"));
+
+  if (cfsetispeed (&tio, rate) < 0)
+    perror_with_name (_("Can not set custom input baud rate"));
+
+  if (cfsetospeed (&tio, rate) < 0)
+    perror_with_name (_("Can not set custom output baud rate"));
+
+  if (tcsetattr (fd, TCSANOW, &tio) < 0)
+    perror_with_name (_("Can not set custom baud rate"));
+}
+
+#endif /* HAVE_CFSETSPEED_ARBITRARY */
+
 #if HAVE_CUSTOM_BAUDRATE_SUPPORT && defined(BOTHER)
 
 /* Set a custom baud rate using the termios BOTHER.  */
@@ -560,14 +590,17 @@ set_custom_baudrate_darwin (int fd, int rate)
 #if HAVE_CUSTOM_BAUDRATE_SUPPORT
 
 /* Set a baud rate that differs from the OS B_codes.
-   This is possible if one of the following macros is available:
-   - BOTHER (Linux).
-   - IOSSIOSPEED (Darwin).  */
+   Prefer the POSIX cfsetispeed/cfsetospeed interface when the host
+   libc accepts arbitrary baud rates.  Otherwise fall back to
+   Linux-specific (BOTHER) or Darwin-specific (IOSSIOSPEED)
+   interfaces.  */
 
 static void
 set_custom_baudrate (int fd, int rate)
 {
-#if defined(BOTHER)
+#if defined(HAVE_CFSETSPEED_ARBITRARY)
+  set_custom_baudrate_posix (fd, rate);
+#elif defined(BOTHER)
   set_custom_baudrate_linux (fd, rate);
 #elif defined(IOSSIOSPEED)
   set_custom_baudrate_darwin (fd, rate);
